Skip to content

Commit b9165a5

Browse files
committed
Fix: alarmclock validation, formatting, and missing function
1 parent b5c66ef commit b9165a5

1 file changed

Lines changed: 29 additions & 41 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,47 @@
1-
function setAlarm() {
2-
const input = document.getElementById("alarmSet").value;
1+
let countdown;
32

4-
if (!input || input <= 0) {
5-
alert("Please enter a valid number");
6-
return;
7-
}
8-
9-
let timeRemaining = parseInt(input);
3+
function startTimer(input) {
104
const display = document.getElementById("timeRemaining");
115

12-
const minutes = Math.floor(timeRemaining / 60);
13-
const seconds = timeRemaining % 60;
6+
// Validate input
7+
const timeRemaining = Number(input);
8+
9+
if (isNaN(timeRemaining) || timeRemaining <= 0) {
10+
display.textContent = "Please enter a valid number";
11+
return;
12+
}
1413

15-
display.textContent = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
14+
let remaining = timeRemaining;
1615

1716
// Clear any previous timer
1817
clearInterval(countdown);
1918

19+
function updateDisplay() {
20+
const minutes = Math.floor(remaining / 60);
21+
const seconds = remaining % 60;
22+
23+
display.textContent = `Time Remaining: ${String(minutes).padStart(
24+
2,
25+
"0"
26+
)}:${String(seconds).padStart(2, "0")}`;
27+
}
28+
29+
// Show initial time
30+
updateDisplay();
31+
2032
countdown = setInterval(() => {
21-
timeRemaining--;
33+
remaining--;
2234

23-
display.textContent = `Time Remaining: 00:${timeRemaining
24-
.toString()
25-
.padStart(2, "0")}`;
35+
updateDisplay();
2636

27-
if (timeRemaining === 0) {
37+
if (remaining <= 0) {
2838
clearInterval(countdown);
2939
playAlarm();
3040
}
3141
}, 1000);
3242
}
3343

34-
// DO NOT EDIT BELOW HERE
35-
36-
let countdown; // 👈 needed for timer control
37-
38-
var audio = new Audio("alarmsound.mp3");
39-
40-
function setup() {
41-
document.getElementById("set").addEventListener("click", () => {
42-
setAlarm();
43-
});
44-
45-
document.getElementById("stop").addEventListener("click", () => {
46-
pauseAlarm();
47-
});
48-
}
49-
44+
// :white_check_mark: Fix missing function
5045
function playAlarm() {
51-
audio.play();
52-
}
53-
54-
function pauseAlarm() {
55-
audio.pause();
56-
clearInterval(countdown);
57-
}
58-
59-
window.onload = setup;
46+
alert("Time's up!");
47+
}

0 commit comments

Comments
 (0)