Skip to content

Commit 44f98d7

Browse files
committed
update alarm logic and removed unused comments
1 parent 0d11d74 commit 44f98d7

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,22 @@ function time_convert(num) {
66
return minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0');
77
}
88
function setAlarm() {
9-
const alarmSetInput = document.getElementById("alarmSet");
10-
const timer = time_convert(alarmSetInput.value)
11-
const timeRemainingCounter = document.getElementById("timeRemaining");
12-
timeRemainingCounter.textContent = `Time Remaining: ${timer}`;
9+
const alarmSetInputEl = document.getElementById("alarmSet");
10+
let timeEl = Number(alarmSetInputEl.value);
11+
const timeRemainingCounterEl = document.getElementById("timeRemaining");
12+
const interval = setInterval(() => {
13+
timeEl--;
14+
15+
timeRemainingCounterEl.textContent = `Time Remaining: ${time_convert(timeEl)}`;
16+
17+
if (timeEl <= 0) {
18+
clearInterval(interval);
19+
timeRemainingCounterEl.textContent = "Done!";
20+
playAlarm();
21+
}
22+
}, 1000);
1323
}
1424

15-
// ## How the clock should work
16-
17-
// When you click the `Set Alarm` button the counter at the top of the screen should change to the number you entered in the `input` field. For example, if the `input` field says `10` then the title should say `Time Remaining: 00:10`. => Done
18-
19-
// Every one second the title should count down by one.
20-
21-
// When the `Time Remaining` reaches `00:00` the alarm should play a sound. You can make the sound happen by using `playAlarm()`.
22-
23-
// You can stop the alarm sound by pressing the `Stop Alarm` button.
24-
25-
// ## Need Help?
26-
27-
// Only read this section if you really need to! It's good to work this out for yourself.
28-
29-
// ### Hints
30-
31-
// - Have you tried looking at the `setInterval` function?
3225

3326
// DO NOT EDIT BELOW HERE
3427

@@ -53,3 +46,4 @@ function pauseAlarm() {
5346
}
5447

5548
window.onload = setup;
49+

0 commit comments

Comments
 (0)