Skip to content

Commit b0576a0

Browse files
committed
Fix input validation and timer handling in alarm clock
1 parent f9ffdd6 commit b0576a0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
function setAlarm() {
2-
let timer;
1+
let timer;
32

3+
function setAlarm() {
44
const input = document.getElementById("alarmSet").value;
55
let timeLeft = Number(input);
66

77
updateDisplay(timeLeft);
88

9+
if (input === "" || isNaN(timeLeft)) {
10+
return;
11+
}
12+
13+
if (timeLeft < 0) {
14+
return;
15+
}
16+
17+
updateDisplay(timeLeft);
18+
919
clearInterval(timer);
1020

1121
timer = setInterval(() => {
@@ -24,6 +34,10 @@ function setAlarm() {
2434
function updateDisplay(secondsLeft) {
2535
const heading = document.getElementById("timeRemaining");
2636

37+
if (secondsLeft < 0) {
38+
secondsLeft = 0;
39+
}
40+
2741
const minutes = Math.floor(secondsLeft / 60);
2842
const seconds = secondsLeft % 60;
2943

0 commit comments

Comments
 (0)