Skip to content

Commit 0f8a136

Browse files
Making small validation change that show on HTML
1 parent 005369b commit 0f8a136

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,35 @@ function resetAlarm() {
77
updateHeading(0);
88
}
99

10+
function showError(message) {
11+
const heading = document.getElementById("timeRemaining");
12+
heading.innerText = message;
13+
}
14+
1015
function setAlarm() {
1116
const input = document.getElementById("alarmSet");
1217
let time = Number(input.value);
1318

19+
// Always reset first so old countdown stops
20+
resetAlarm();
21+
22+
// Validate input (reject decimals, negatives, NaN)
1423
if (isNaN(time) || time < 0 || !Number.isInteger(time)) {
15-
updateHeading(0);
24+
showError("Please enter a whole number of seconds.");
1625
return;
1726
}
1827

19-
resetAlarm();
20-
28+
// If time is 0, trigger alarm immediately
2129
if (time === 0) {
2230
updateHeading(0);
2331
playAlarm();
2432
return;
2533
}
2634

35+
// Show starting time
2736
updateHeading(time);
2837

38+
// Start countdown
2939
countdown = setInterval(() => {
3040
time--;
3141

0 commit comments

Comments
 (0)