Skip to content

Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 3 | Grouping data: Alarm Clock#1182

Open
bytesandroses wants to merge 18 commits intoCodeYourFuture:mainfrom
bytesandroses:coursework/Sprint-3/alarm-clock
Open

Cape Town | 2026-ITP-Jan | Isaac Abodunrin | Sprint 3 | Grouping data: Alarm Clock#1182
bytesandroses wants to merge 18 commits intoCodeYourFuture:mainfrom
bytesandroses:coursework/Sprint-3/alarm-clock

Conversation

@bytesandroses
Copy link
Copy Markdown
Member

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Implemented alarm clock functionality and countdown timer logic to

  • set alarm
  • format time correctly
  • display the remaining time as it counts down

@bytesandroses bytesandroses added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 6, 2026

function setAlarm() {
const setTime = document.getElementById("alarmSet").value;
timeRemaining = parseInt(setTime, 10);
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.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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:

  1. Ensure the input is a number
  2. Ensure the number is positive

Comment on lines +33 to +35
if (timerInterval) {
clearInterval(timerInterval);
}
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 9, 2026

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

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.

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.

Comment on lines +16 to +27
function decreaseAlarmTime() {
if (timeRemaining <= 0) {
clearInterval(timerInterval);
timerInterval = null;
timeRemaining = 0;
playAlarm();
return;
}

timeRemaining--;
displayAlarm(timeRemaining);
}
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.

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.

Copy link
Copy Markdown
Member Author

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: timeRemaining is first decremented so that the alarm sound can be played at exactly 00:00 without needing to start a new loop.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Apr 9, 2026
@bytesandroses bytesandroses added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 11, 2026
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

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

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

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 11, 2026
@bytesandroses
Copy link
Copy Markdown
Member Author

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

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 playAlarm() to check whether the user has inputted a value of 0. If it's 0, then the alarm sound is automatically played without the 1 second delay. Otherwise the countdown is run as usual.

@bytesandroses bytesandroses added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 11, 2026
@cjyuan
Copy link
Copy Markdown
Contributor

cjyuan commented Apr 11, 2026

Changes look good.

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 11, 2026
@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Apr 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants