Skip to content

Commit aded465

Browse files
author
JorvanW
committed
Updated code for alarmclock.js
1 parent 09a5ef1 commit aded465

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const timeRemaining = document.getElementById("timeRemaining");
22
let timer;
3+
let seconds;
34

45
function setAlarm() {
56
const alarmSetInput = document.getElementById("alarmSet");
6-
let seconds = Number(alarmSetInput.value);
7+
seconds = Number(alarmSetInput.value);
8+
79

810
if (seconds <= 0 || isNaN(seconds)) {
911
alert("Please enter a valid number.");
@@ -13,27 +15,29 @@ function setAlarm() {
1315
// Stop a previous countdown if there is one
1416
clearInterval(timer);
1517

16-
timer = setInterval(() => {
18+
// Run immediately
19+
decrementTimer();
1720

18-
const minutes = Math.floor(seconds / 60);
19-
const secs = seconds % 60;
21+
// decrement each second
22+
timer = setInterval(() => decrementTimer(), 1000);
23+
}
2024

21-
timeRemaining.textContent =
22-
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
25+
function decrementTimer()
26+
{
27+
const minutes = Math.floor(seconds / 60);
28+
const secs = seconds % 60;
2329

24-
if (seconds <= 0) {
25-
clearInterval(timer);
26-
playAlarm();
27-
return;
28-
}
30+
timeRemaining.textContent = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
2931

30-
seconds--;
32+
if (seconds <= 0) {
33+
clearInterval(timer);
34+
playAlarm();
35+
return;
36+
}
3137

32-
}, 1000);
38+
seconds--;
3339
}
3440

35-
document.getElementById("set").addEventListener("click", setAlarm);
36-
3741
// DO NOT EDIT BELOW HERE
3842

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

0 commit comments

Comments
 (0)