Skip to content
36 changes: 35 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
function setAlarm() {}
let timeLeft = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could declare timeLeft inside setAlarm() (since it is only used within that function).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I’ve moved timeLeft inside setAlarm to limit its scope and keep the global space cleaner.

Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still declared in the outermost scope.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — I had left the global timeLeft in by mistake. I’ve removed it and kept it scoped inside setAlarm() 👍

let timer = null;

function formatTime(seconds) {
let mins = String(Math.floor(seconds / 60)).padStart(2, "0");
let secs = String(seconds % 60).padStart(2, "0");
return `${mins}:${secs}`;
}

function setAlarm() {
const input = document.getElementById("alarmSet").value;
const display = document.getElementById("timeRemaining");

timeLeft = parseInt(input, 10);

if (isNaN(timeLeft) || timeLeft <= 0) {
alert("Please enter a valid number of seconds.");
return;
}

display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
Comment thread
cjyuan marked this conversation as resolved.
Outdated

if (timer) {
clearInterval(timer);
}
Comment on lines +24 to +27
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What elsse should also be reset before a new countdown begin?

Hint: a user may not click the "Stop" button first before starting a new count down.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point I’ve reset the timer reference to null after clearing it to ensure a clean state before starting a new countdown.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Setting timer to null is unnecessary in JS but it is a good practice.

  • What does the "Stop" button do? What would happen when a countdown reaches zero, and the user immediately set another countdown?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Stop button clears the timer and stops the alarm. If a countdown reaches zero and the user immediately starts another one, the alarm may still be playing. So we should also reset/stop the alarm when starting a new countdown to keep the behavior consistent.

timer = setInterval(() => {
if (timeLeft > 0) {
timeLeft--;
display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
} else {
clearInterval(timer);
playAlarm();
}
}, 1000);
}
Comment thread
cjyuan marked this conversation as resolved.

// DO NOT EDIT BELOW HERE

Expand Down
Loading