Skip to content

Commit 2e2d451

Browse files
committed
Validate countdown input
1 parent ea108ca commit 2e2d451

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
1-
function displayTime(time) {
2-
const remainingElement = document.getElementById("timeRemaining");
3-
4-
const minutes = String(Math.floor(time / 60)).padStart(2, "0");
5-
const seconds = String(time % 60).padStart(2, "0");
1+
let interval;
62

7-
remainingElement.innerText = `Time Remaining: ${minutes}:${seconds}`;
3+
function displayTime(time) {
4+
const remainingElement = document.getElementById("timeRemaining");
5+
const minutes = String(Math.floor(time / 60)).padStart(2, "0");
6+
const seconds = String(time % 60).padStart(2, "0");
7+
remainingElement.innerText = `Time Remaining: ${minutes}:${seconds}`;
88
}
99

1010
function setAlarm() {
11-
let time = parseInt(document.getElementById("alarmSet").value);
12-
displayTime(time);
11+
let time = Number(document.getElementById("alarmSet").value);
12+
13+
if (isNaN(time) || time <= 0) {
14+
return;
15+
}
16+
17+
clearInterval(interval);
18+
pauseAlarm();
1319

14-
const interval = setInterval(() => {
15-
time--;
16-
displayTime(time);
17-
if (time === 0) {
18-
clearInterval(interval);
19-
playAlarm();
20+
if (!Number.isInteger(time) || time <= 0) {
21+
return;
2022
}
21-
}, 1000);
22-
}
2323

24+
displayTime(time);
2425

26+
interval = setInterval(() => {
27+
time--;
28+
displayTime(time);
2529

30+
if (time === 0) {
31+
clearInterval(interval);
32+
playAlarm();
33+
}
34+
}, 1000);
35+
}
2636

37+
window.addEventListener("load", () => {
38+
document.getElementById("stop").addEventListener("click", () => {
39+
clearInterval(interval);
40+
});
41+
});
2742

2843

2944
// DO NOT EDIT BELOW HERE

0 commit comments

Comments
 (0)