Skip to content

Commit 6ef6002

Browse files
committed
Implement setAlarm() with countdown timer and fix template literal syntax error
1 parent de6889c commit 6ef6002

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
function setAlarm() {}
1+
function setAlarm() {
2+
const input = document.getElementById("alarmSet");
3+
const heading = document.getElementById("timeRemaining");
4+
5+
let secondsRemaining = Number(input.value);
6+
7+
function updateDisplay() {
8+
const minutes = Math.floor(secondsRemaining / 60);
9+
const seconds = secondsRemaining % 60;
10+
const paddedMinutes = String(minutes).padStart(2, "0");
11+
const paddedSeconds = String(seconds).padStart(2, "0");
12+
heading.innerText = `Time Remaining: ${paddedMinutes}:${paddedSeconds}`;
13+
}
14+
15+
updateDisplay();
16+
17+
const intervalId = setInterval(() => {
18+
secondsRemaining--;
19+
updateDisplay();
20+
21+
if (secondsRemaining <= 0) {
22+
clearInterval(intervalId);
23+
playAlarm();
24+
}
25+
}, 1000);
26+
}
227

328
// DO NOT EDIT BELOW HERE
429

@@ -22,4 +47,4 @@ function pauseAlarm() {
2247
audio.pause();
2348
}
2449

25-
window.onload = setup;
50+
window.onload = setup;

0 commit comments

Comments
 (0)