-
-
Notifications
You must be signed in to change notification settings - Fork 281
Cape Town | 2026-ITP-Jan | Pretty Taruvinga | Sprint 3 | Alarm clock #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
53fa90f
ef66a7c
ebb2f79
9fdb334
6cb11f7
6b7e254
948fa59
e54e3fc
ae98f15
bdb176b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,38 @@ | ||
| function setAlarm() {} | ||
| let timeLeft = 0; | ||
| 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)}`; | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (timer) { | ||
| clearInterval(timer); | ||
| } | ||
|
Comment on lines
+24
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could declare
timeLeftinsidesetAlarm()(since it is only used within that function).There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() 👍