Skip to content

Commit e57493f

Browse files
committed
inconsistencies addressed: math.max removed, and alarm sound edge case addressed
1 parent feb5c66 commit e57493f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ function updateDisplay() {
1818
const heading = document.getElementById("timeRemaining");
1919
const input = document.getElementById("alarmSet");
2020

21-
totalSeconds = Math.max(0, totalSeconds);
2221
const formatted = formatTime(totalSeconds);
2322

2423
input.value = formatted;
2524
heading.innerHTML = `Time Remaining:<br><br>${formatted}`;
2625
}
2726

2827
function incrementTime(amount) {
29-
totalSeconds = Math.max(0, totalSeconds + amount);
28+
totalSeconds += amount;
29+
if (totalSeconds < 0) totalSeconds = 0;
3030
updateDisplay();
3131
}
3232

@@ -51,16 +51,22 @@ function setAlarm() {
5151
totalSeconds = parsed;
5252
updateDisplay();
5353

54-
timer = setInterval(() => {
55-
totalSeconds--;
56-
totalSeconds = Math.max(0, totalSeconds);
57-
updateDisplay();
54+
if (totalSeconds === 0) {
55+
playAlarm();
56+
return;
57+
} //Alarm plays immediately if time is set to 00:00
5858

59-
if (totalSeconds === 0) {
59+
timer = setInterval(() => {
60+
totalSeconds -= 1;
61+
if (totalSeconds <= 0) {
62+
totalSeconds = 0;
63+
updateDisplay();
6064
clearInterval(timer);
6165
timer = null;
6266
playAlarm();
67+
return;
6368
}
69+
updateDisplay();
6470
}, 1000);
6571
}
6672

0 commit comments

Comments
 (0)