-
-
Notifications
You must be signed in to change notification settings - Fork 283
Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 3 | Grouping data: Alarm Clock #1182
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 14 commits
104bc75
bc9db0d
2e9abac
15cd7d0
01299b1
13a05e6
7a55810
d0daf34
47e2f22
5fcb0b1
98a0a64
dfa1f57
cb65771
eeb1c24
f165cf5
e903b09
76e9b57
5386aff
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,6 +1,42 @@ | ||
| function setAlarm() {} | ||
| let timeRemaining; | ||
| let timerInterval; | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| function formatTime(time) { | ||
| const minutes = String(Math.floor(time / 60)).padStart(2, "0"); | ||
| const seconds = String(time % 60).padStart(2, "0"); | ||
|
|
||
| return `${minutes}:${seconds}`; | ||
| } | ||
|
|
||
| function displayAlarm(time) { | ||
| const alarmBox = document.getElementById("timeRemaining"); | ||
| alarmBox.textContent = `Time Remaining: ${formatTime(time)}`; | ||
| } | ||
|
|
||
| function decreaseAlarmTime() { | ||
| if (timeRemaining <= 0) { | ||
| clearInterval(timerInterval); | ||
| timerInterval = null; | ||
| timeRemaining = 0; | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| timeRemaining--; | ||
| displayAlarm(timeRemaining); | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| const setTime = document.getElementById("alarmSet").value; | ||
| timeRemaining = parseInt(setTime, 10); | ||
|
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. Using user input without proper validation and sanitisation can be dangeours. Currently, some unusual input values can make your app behave abnormally. Can you add code to sanitise them?
Member
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. You're absolutely right -- the timer behaves oddly if certain inputs are automatically allowed without sanitizing. If a user enters a negative number, a negative timer is displayed which can't be counted down. And if a number isn't entered, it simply displays NaNs. To fix this, I've added two validation checks:
|
||
|
|
||
| if (timerInterval) { | ||
| clearInterval(timerInterval); | ||
| } | ||
|
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. The countdown interval is not the only state to be reset. Hint: a user may not click the "Stop" button first before starting a new count down. You can also consider introducing a dedicated reset function to return the app to a clean initial state to help ensure consistency.
Member
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. I see what you mean. The I've taken your suggestion and handled it in s single
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. I meant to point out "alarm not reset", which you have handled now in the In JS, calling |
||
|
|
||
| displayAlarm(timeRemaining); | ||
| timerInterval = setInterval(decreaseAlarmTime, 1000); | ||
| } | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
|
|
||
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.
When the countdown reaches 00:00, there is a one second delay before the alarm sound is played. Is this by design?
At line 25, when
timeRemainingchanges from 1 to 0, the app only changes the display to 00:00. It's only in the next interval, the code on lines 17-22 is executed.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.
No this definitely a bug -- it wasn't by design.
I've applied a fix by changing the order of execution:
timeRemainingis first decremented so that the alarm sound can be played at exactly 00:00 without needing to start a new loop.