Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 3 | Grouping data: Alarm Clock#1182
Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 3 | Grouping data: Alarm Clock#1182bytesandroses wants to merge 18 commits intoCodeYourFuture:mainfrom
Conversation
Sprint-3/alarmclock/alarmclock.js
Outdated
|
|
||
| function setAlarm() { | ||
| const setTime = document.getElementById("alarmSet").value; | ||
| timeRemaining = parseInt(setTime, 10); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
- Ensure the input is a number
- Ensure the number is positive
Sprint-3/alarmclock/alarmclock.js
Outdated
| if (timerInterval) { | ||
| clearInterval(timerInterval); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I see what you mean. The clearInterval(timerInterval) only stops the timer counting down -- it doesn't remove it. So it creates a situation where multiple timers can run at the same time.
I've taken your suggestion and handled it in s single reset() function.
There was a problem hiding this comment.
I meant to point out "alarm not reset", which you have handled now in the reset() function.
In JS, calling clearInterval(timerInterval) is enough to remove the active interval.
Performing timerInterval = null; is optional but a good practice.
| function decreaseAlarmTime() { | ||
| if (timeRemaining <= 0) { | ||
| clearInterval(timerInterval); | ||
| timerInterval = null; | ||
| timeRemaining = 0; | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| timeRemaining--; | ||
| displayAlarm(timeRemaining); | ||
| } |
There was a problem hiding this comment.
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 timeRemaining changes 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.
No this definitely a bug -- it wasn't by design.
I've applied a fix by changing the order of execution: timeRemaining is first decremented so that the alarm sound can be played at exactly 00:00 without needing to start a new loop.
There was a problem hiding this comment.
When input is 0, there is a delay of one second before the alarm is sounded, and the timer display will mess up.
Note: In setAlarm()
// This only setups decreaseAlarmTime() to be called one second later (and every second thereafter)
timerInterval = setInterval(decreaseAlarmTime, 1000);
displayAlarm(timeRemaining); // This line is executed first before decreaseAlarmTim() is called…s, without 1 second delay
Thank CJ, you're absolutely correct. There's a bug when a user enters a value of 0. To fix it, I've added an extra step in |
|
Changes look good. |
Self checklist
Changelist
Implemented alarm clock functionality and countdown timer logic to