Skip to content

Commit 2cd628f

Browse files
committed
use const instead let in the updateDisplay function \n decrement the remainSecond before calling updateDisplay \n handle zero and negative-input
1 parent ccf9c34 commit 2cd628f

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ let timerId = null;
22
let remainingSeconds = 0;
33

44
function updateDisplay() {
5-
let minutes = String(Math.floor(remainingSeconds / 60)).padStart(2, "0");
6-
let seconds = String(remainingSeconds % 60).padStart(2, "0");
5+
const minutes = String(Math.floor(remainingSeconds / 60)).padStart(2, "0");
6+
const seconds = String(remainingSeconds % 60).padStart(2, "0");
77
document.getElementById(
88
"timeRemaining"
99
).textContent = `Time Remaining: ${minutes}:${seconds}`;
1010
}
1111

1212
function startCountdown() {
13-
if (remainingSeconds < 0) remainingSeconds = 0;
1413
timerId = setInterval(() => {
14+
remainingSeconds--;
1515
updateDisplay();
1616

1717
if (remainingSeconds <= 0) {
@@ -20,20 +20,24 @@ function startCountdown() {
2020
timerId = null;
2121
playAlarm();
2222
}
23-
remainingSeconds -= 1;
2423
}, 1000);
2524
}
2625

2726
function setAlarm() {
2827
const input = document.getElementById("alarmSet");
2928
if (input.value === "") return;
30-
const inputValue = Number(input.value);
29+
remainingSeconds = Number(input.value);
3130
clearInterval(timerId);
3231
timerId = null;
33-
remainingSeconds = inputValue;
3432

35-
updateDisplay();
36-
startCountdown();
33+
if (remainingSeconds <= 0) {
34+
remainingSeconds = 0;
35+
updateDisplay();
36+
playAlarm();
37+
} else {
38+
updateDisplay();
39+
startCountdown();
40+
}
3741
input.value = "";
3842
}
3943

0 commit comments

Comments
 (0)