Skip to content

Commit 0df1cfb

Browse files
committed
corrections after second review
1 parent 0a1c10e commit 0df1cfb

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1-
let countdown;
1+
let countdownId;
2+
3+
// moved to outer scope, takes seconds as a parameter
4+
function updateDisplay(seconds) {
5+
const minutes = Math.floor(seconds / 60);
6+
const remainingSeconds = seconds % 60;
7+
document.getElementById("timeRemaining").textContent =
8+
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
9+
}
210

311
// reset before starting new countdown
412
function resetAlarm() {
5-
clearInterval(countdown);
6-
audio.pause();
7-
document.getElementById("timeRemaining").textContent = "Time Remaining: 00:00";
13+
clearInterval(countdownId);
14+
updateDisplay(0); // replaces the manual textContent line
815
document.body.classList.toggle("alarm-activated", false);
916
}
1017

1118
function setAlarm() {
1219
let seconds = parseInt(document.getElementById("alarmSet").value);
1320

1421
if (!seconds || seconds < 1) {
15-
alert("The number of seconds must be more than 0 please");
22+
alert("The number of seconds must be higher than 0 please");
1623
return;
1724
}
18-
function updateDisplay() {
19-
const minutes = Math.floor(seconds / 60);
20-
const remainingSeconds = seconds % 60;
21-
document.getElementById("timeRemaining").textContent =
22-
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
23-
}
24-
25-
updateDisplay(); // update immediately on click
2625

27-
// code to reset background
28-
function pauseAlarm() {
29-
audio.pause();
30-
document.body.classList.toggle("alarm-activated", false);
31-
}
26+
updateDisplay(seconds);
27+
// pass seconds as argument and update immediately on click
3228

33-
countdown = setInterval(() => {
29+
countdownId = setInterval(() => {
3430
seconds--;
35-
updateDisplay();
31+
updateDisplay(seconds);
32+
// pass seconds as argument
3633

3734
if (seconds <= 0) {
38-
clearInterval(countdown);
35+
clearInterval(countdownId);
3936
playAlarm();
4037
document.body.classList.toggle("alarm-activated", true);
4138
}

0 commit comments

Comments
 (0)