Skip to content

Commit b2c81bf

Browse files
function for alarm clock
1 parent 10b3ac9 commit b2c81bf

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,48 @@
11
function setAlarm() {}
2+
// Only define Audio if it doesn't exist (Node environment)
3+
if (typeof Audio === "undefined") {
4+
global.Audio = class {
5+
constructor(src) {
6+
this.src = src;
7+
}
8+
play() {}
9+
pause() {}
10+
};
11+
}
12+
13+
let intervalId;
14+
15+
const formatTime = (s) => {
16+
const mins = Math.floor(s / 60);
17+
const secs = s % 60;
18+
return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
19+
};
20+
21+
function setAlarm() {
22+
const input = document.getElementById("alarmSet");
23+
const timer = document.getElementById("timeRemaining");
24+
25+
const seconds = parseInt(input.value);
26+
if (isNaN(seconds) || seconds < 0) return;
27+
28+
let remaining = seconds;
29+
30+
// DO NOT EDIT BELOW HERE
31+
timer.textContent = `Time Remaining: ${formatTime(remaining)}`;
32+
33+
if (intervalId) clearInterval(intervalId);
34+
35+
intervalId = setInterval(() => {
36+
remaining--;
37+
38+
timer.textContent = `Time Remaining: ${formatTime(remaining)}`;
39+
40+
if (remaining <= 0) {
41+
clearInterval(intervalId);
42+
playAlarm();
43+
}
44+
}, 1000);
45+
}
246

347
// DO NOT EDIT BELOW HERE
448

0 commit comments

Comments
 (0)