Skip to content

Commit debc327

Browse files
Complete alarm clock
1 parent 96d077b commit debc327

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
1-
function setAlarm() {}
1+
function setAlarm() {
2+
let timeRemaining = document.getElementById("alarmSet").value;
3+
const timeDisplay = document.getElementById("timeRemaining");
4+
5+
// Create a reusable function to update the text on the screen
6+
const updateDisplay = (time) => {
7+
let minutes = Math.floor(time / 60)
8+
.toString()
9+
.padStart(2, "0");
10+
let seconds = (time % 60).toString().padStart(2, "0");
11+
timeDisplay.innerText = "Time Remaining: " + minutes + ":" + seconds;
12+
};
13+
14+
// STEP 1: Display the starting time IMMEDIATELY
15+
updateDisplay(timeRemaining);
16+
17+
// STEP 2: Start the interval
18+
const countdown = setInterval(() => {
19+
timeRemaining--;
20+
updateDisplay(timeRemaining);
21+
22+
if (timeRemaining <= 0) {
23+
clearInterval(countdown);
24+
playAlarm();
25+
}
26+
}, 1000);
27+
}
228

329
// DO NOT EDIT BELOW HERE
430

0 commit comments

Comments
 (0)