Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 82 additions & 5 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,94 @@
function setAlarm() {}
// DOM elements
const setAlarmBtn = document.getElementById("set");
const stopAlarmBtn = document.getElementById("stop");
const timeRemaining = document.getElementById("timeRemaining");
let alarmInterval;

// Update Display function;
function updateDisplay(secondsValue) {
let minutes = Math.floor(secondsValue / 60);
let seconds = secondsValue % 60;
timeRemaining.innerText = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}

// reset function - returns app to clean initial state
function resetAlarm() {
function resetToInitialState() {
updateDisplay(0);
document.getElementById("alarmSet").value = "";
stopAlarmBtn.style.display = "none";
setAlarmBtn.style.display = "inline-block"; // ADD THIS - show set button
Comment on lines +19 to +20
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also consider

  1. using visibility: hidden (suggestion: look up the difference between display: none and visibility: false
  2. disable/enable the buttons by setting their disabled property

(No change required)

}

function stopSound() {
if (typeof pauseAlarm === "function") {
pauseAlarm();
}
}
Comment thread
cjyuan marked this conversation as resolved.
// Clear any running interval
if (alarmInterval) {
clearInterval(alarmInterval);
alarmInterval = null;
}

resetToInitialState();
stopSound();
}

// Set Alarm function
function setAlarm() {
let totalSeconds;
const input = document.getElementById("alarmSet");
const timeValue = parseInt(input.value);
const hasActiveTimer = alarmInterval !== null;

function startTimer() {
alarmInterval = setInterval(() => {
totalSeconds--;
updateDisplay(totalSeconds);
if (totalSeconds === 0) {
clearInterval(alarmInterval);
alarmInterval = null;

// WHEN ALARM STARTS: hide set button, show only stop button
setAlarmBtn.style.display = "none";
stopAlarmBtn.style.display = "inline-block";

playAlarm();
}
}, 1000);
}

resetAlarm();

if (isNaN(timeValue) || timeValue <= 0) {
if (!hasActiveTimer) alert("Please enter a number greater than 0");
return;
}

totalSeconds = timeValue;
stopAlarmBtn.style.display = "inline-block";
setAlarmBtn.style.display = "inline-block"; // Ensure set button is visible
updateDisplay(totalSeconds);
startTimer();
}

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
resetAlarm();
});

// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");

function setup() {
// Ensure clean initial state when page loads
resetAlarm();

document.getElementById("set").addEventListener("click", () => {
setAlarm();
});

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
Comment on lines -11 to -14
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to delete this. You can just perform resetAlarm() in the event listener you introduced on line 76.

}

function playAlarm() {
Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm Clock</title>
</head>
<body>
<div class="centre">
Expand Down
2 changes: 2 additions & 0 deletions Sprint-3/alarmclock/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// jest.setup.js
require("@testing-library/jest-dom/extend-expect");
14 changes: 12 additions & 2 deletions Sprint-3/alarmclock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"license": "CC-BY-SA-4.0",
"description": "You must update this package",
"scripts": {
"test": "jest --config=../jest.config.js alarmclock"
"test": "jest"
},
"jest": {
"setupFilesAfterEnv": [
"<rootDir>/jest.setup.js"
]
},
"repository": {
"type": "git",
Expand All @@ -13,5 +18,10 @@
"bugs": {
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"jest": "^27.5.1",
"jsdom": "^19.0.0"
}
}
5 changes: 5 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
h1 {
text-align: center;
}

/* Both buttons visible by default, but resetAlarm will hide stop initially */
#stop {
display: none;
}
Loading