Skip to content

Commit f9ffdd6

Browse files
committed
Add working alarm clock with countdown and sound
1 parent 10b3ac9 commit f9ffdd6

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Sprint-3/alarmclock/alarmclock.js

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

333
// DO NOT EDIT BELOW HERE
434

0 commit comments

Comments
 (0)