Skip to content

Commit f691b29

Browse files
committed
keep remainingSeconds local to setAlarm
1 parent 7ead5c3 commit f691b29

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
let timerInterval = null;
2-
let remainingSeconds = 0;
32

43
// Resets timer state
54
function resetTimer() {
65
if (timerInterval !== null) {
76
clearInterval(timerInterval);
87
timerInterval = null;
98
}
10-
remainingSeconds = 0;
119
}
1210
//Starts the alarm counbtdown
1311
function setAlarm() {
1412
// Read the minutes value from the alarm input field
1513
const minutesInput = document.getElementById("alarmSet");
1614
const seconds = parseInt(minutesInput.value, 10);
15+
1716
// Ignore invalid or non-positive input
1817

1918
if (isNaN(seconds) || seconds <= 0) {
@@ -22,28 +21,28 @@ function setAlarm() {
2221
// Reset any existing timer first
2322
resetTimer();
2423

25-
// Store the input as the remaining seconds to count down
26-
remainingSeconds = seconds;
24+
//local variable (no global sharing)
25+
let remainingSeconds = seconds;
2726

2827
// Update display immediately
29-
updateTimeDisplay();
28+
updateTimeDisplay(remainingSeconds);
3029

3130
// Start countdown
3231
timerInterval = setInterval(() => {
3332
remainingSeconds--;
3433

35-
updateTimeDisplay();
34+
updateTimeDisplay(remainingSeconds);
3635

3736
if (remainingSeconds <= 0) {
3837
resetTimer();
39-
updateTimeDisplay();
38+
updateTimeDisplay(0);
4039
playAlarm();
4140
}
4241
}, 1000);
4342
}
4443

4544
// Converts remainingSeconds into MM:SS format and updates the display
46-
function updateTimeDisplay() {
45+
function updateTimeDisplay(remainingSeconds) {
4746
const minutes = Math.floor(remainingSeconds / 60);
4847
const seconds = remainingSeconds % 60;
4948

0 commit comments

Comments
 (0)