Skip to content

Commit ebb2f79

Browse files
author
Pretty Taruvinga
committed
revert to main
1 parent ef66a7c commit ebb2f79

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
function setAlarm() {}
1+
let timeLeft = 0;
2+
let timer = null;
3+
4+
function formatTime(seconds) {
5+
let mins = String(Math.floor(seconds / 60)).padStart(2, "0");
6+
let secs = String(seconds % 60).padStart(2, "0");
7+
return `${mins}:${secs}`;
8+
}
9+
10+
function setAlarm() {
11+
const input = document.getElementById("alarmSet").value;
12+
const display = document.getElementById("timeRemaining");
13+
14+
timeLeft = parseInt(input, 10);
15+
16+
if (isNaN(timeLeft) || timeLeft <= 0) {
17+
alert("Please enter a valid number of seconds.");
18+
return;
19+
}
20+
21+
display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
22+
23+
if (timer) {
24+
clearInterval(timer);
25+
}
26+
timer = setInterval(() => {
27+
if (timeLeft > 0) {
28+
timeLeft--;
29+
display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
30+
} else {
31+
clearInterval(timer);
32+
playAlarm();
33+
}
34+
}, 1000);
35+
}
236

337
// DO NOT EDIT BELOW HERE
438

0 commit comments

Comments
 (0)