Skip to content

London | 26-ITP-Jan | Carlos Abreu |Sprint 3 | Alarm Clock#1140

Open
carlosyabreu wants to merge 8 commits intoCodeYourFuture:mainfrom
carlosyabreu:coursework/sprint-3/alarmclock
Open

London | 26-ITP-Jan | Carlos Abreu |Sprint 3 | Alarm Clock#1140
carlosyabreu wants to merge 8 commits intoCodeYourFuture:mainfrom
carlosyabreu:coursework/sprint-3/alarmclock

Conversation

@carlosyabreu
Copy link
Copy Markdown

Learners, PR Template

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

Alarm clock app PR for Sprint 3 module

@github-actions

This comment has been minimized.

@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 28, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 28, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 29, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 29, 2026
@cjyuan cjyuan added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 30, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 30, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 30, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 30, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 31, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 31, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 31, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 31, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 31, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 31, 2026
@carlosyabreu carlosyabreu force-pushed the coursework/sprint-3/alarmclock branch from 48320e9 to 99099b1 Compare April 1, 2026 17:36
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 1, 2026
@@ -1,4 +1,52 @@
function setAlarm() {}
let timerInterval = null;
let currentTime = 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.

Why not declare currentTime as a local variable inside setAlarm()?

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.

Sorry @cjyuan for the delay.
I've been busy and stressed working on last and final module (Moduel Data Flows) and the Project-TV-Show with a colleague and working in a collaborative way with Git and GitHub has been a hell to understand its workflow.
It's been challenging but rewarding to finally get a glimpse how it works in a shared environment.
As I'm almost done with Module Data Flows and Project TV Show (I'm at level 500 just to finish today later) I'm back to Module Data Group.

Going back to your query.
The reason currentTime is declared outside setAlarm() (at the module level) is that it needs to be shared and changed by multiple functions:
setAlarm() initializes currentTime and starts the interval that decrements it.
The interval callback inside setAlarm() updates currentTime every second.
updateTimeDisplay() reads currentTime to format and display the remaining time.
pauseAlarm() resets currentTime to 0 and updates the display.

If currentTime were declared as a local variable inside setAlarm(), it would be inaccessible to the interval callback (unless captured via closure, but then it couldn't be modified by pauseAlarm() or reset across multiple alarm sets).
Each call to setAlarm() would create a new, independent local variable, breaking the ability to stop or reset the timer from outside. By making it a module level variable, all relevant functions share a single source of true for the remaining time, allowing consistent updates and control.

Comment on lines +5 to +10
// Clear any existing timer
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}

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.

Currently when starting a new countdown, the application does not always return to a clean initial state,
which can lead to inconsistent behaviour between runs.

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

Can also consider introducing a dedicated reset function to return the app to a clean initial state to help ensure consistency. There are few places in this script you can call this reset function instead of repeating the reset logic.

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 pointing that out to me.

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.

Have you figured out what else need to be reset before a new countdown begins?

// Validate input (if empty or invalid, default to 0)
if (isNaN(totalSeconds)) {
totalSeconds = 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.

What range of integers are acceptable? (Currently some input can make the app behave abnormally)

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 acceptable range of integers for the alarm clock is 0 or any positive integer (i.e., totalSeconds >= 0).
Negative integers cause abnormal behavior because:
The updateTimeDisplay function will produce malformed strings like -1:-1 (since Math.floor(-1/60) = -1 and -1 % 60 = -1).
The countdown interval checks if (currentTime > 0), which is false for negative values, so the timer never counts down and never triggers the alarm.
Zero is acceptable: it displays 00:00 and does not play the alarm (as expected for an already‑expired timer). Non‑integer numbers are truncated by parseInt, so only the integer part matters.

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.

Why not just treat negative input as if they are NaN or just return immediately?

Comment on lines +23 to +41
updateTimeDisplay(currentTime);

// Start the countdown
timerInterval = setInterval(() => {
if (currentTime > 0) {
currentTime--;
updateTimeDisplay(currentTime);

// Check if timer reached zero
if (currentTime === 0) {
clearInterval(timerInterval);
timerInterval = null;
playAlarm();
// Change background color
document.body.style.backgroundColor = "red";
}
}
}, 1000);
}
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.

If input is 0, the timer interval will remain active even though we won't notice anything visually.

Copy link
Copy Markdown
Author

@carlosyabreu carlosyabreu Apr 22, 2026

Choose a reason for hiding this comment

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

Thanks for pointing that out it makes the understanding more clear to me.
Looking again to the code I don't think so, I think it is not correct to have the timer interval remain active when the input is 0.
When totalSeconds = 0, the interval starts running but the condition if (currentTime > 0) is never true, so the interval never clears itself. This results in a useless, permanent interval that consumes computer resources unnecessarily.
The expected behavior should be:
if the input is 0, the display should show 00:00 and no interval should be started (and no alarm should play).

Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 22, 2026

Choose a reason for hiding this comment

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

I just noticed your check on line 27.

Different issue now. When input is 0. the alarm won't sound but the interval won't stop.
Visually the user may not notice anything but the active interval will silently consume (very tiny amount) of CPU.

audio.pause();
audio.currentTime = 0;
// Reset background color when alarm is stopped
document.body.style.backgroundColor = "";
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.

To better separate presentation logic from application logic, you can consider defining a CSS class, and use classList.toggle() to apply/remove the style. For example,

document.body.classList.toggle("alarm-activated", true);  // apply style
document.body.classList.toggle("alarm-activated", false); // remove style

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.

Thank you @cjyuan.
Your observation adds value to my understanding.

@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 8, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 22, 2026
}

// Reset display to 00:00
currentTime = 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.

Delete line 84 and then move the declaration of currentTime from line 2 to inside setAlarm(), and your code will still work.

No change required. You can take time to find out why later. It will help your understanding of the JS language better.

@cjyuan
Copy link
Copy Markdown
Contributor

cjyuan commented Apr 22, 2026

Your app is working but the code has some room to improve.

I will mark this PR as complete first. You can address the comments later.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Reviewed Volunteer to add when completing a review with trainee action still to take. labels Apr 22, 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