Skip to content

Commit c5122f9

Browse files
build the function and ready for PR
1 parent d77934a commit c5122f9

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
1-
function setAlarm() {}
1+
let countdown = null;
22

3+
function setAlarm() {
4+
const input = document.getElementById("alarmSet");
5+
let time = Number(input.value);
6+
7+
const heading = document.getElementById("timeRemaining");
8+
9+
if (countdown) {
10+
clearInterval(countdown);
11+
}
12+
13+
updateHeading(time, heading);
14+
15+
countdown = setInterval(() => {
16+
time--;
17+
18+
if (time <= 0) {
19+
updateHeading(0, heading);
20+
clearInterval(countdown);
21+
playAlarm();
22+
} else {
23+
updateHeading(time, heading);
24+
}
25+
}, 1000);
26+
}
27+
28+
function updateHeading(time, heading) {
29+
const minutes = Math.floor(time / 60);
30+
const seconds = time % 60;
31+
32+
const mm = String(minutes).padStart(2, "0");
33+
const ss = String(seconds).padStart(2, "0");
34+
35+
heading.innerText = `Time Remaining: ${mm}:${ss}`;
36+
}
337
// DO NOT EDIT BELOW HERE
438

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

0 commit comments

Comments
 (0)