-
-
Notifications
You must be signed in to change notification settings - Fork 273
London |ITP- Jan-2026| Damian Dunkley |Sprint 3 |Alarm Clock app #1028
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 all commits
7df742b
508a059
e24cff1
e3a54b6
bdd65f9
4fd4b81
b3d03d6
99e8139
4ddc2eb
39e4ac5
57f5291
5af3ad4
038c1c5
8c88fd0
78d5a6a
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,70 @@ | ||
| function setAlarm() {} | ||
| let countdownId = null; | ||
| const originalBackgroundColor = document.body.style.backgroundColor || "white"; // Store the original background color | ||
| let timeRemainingHeading = null; | ||
|
|
||
| function getTimeRemainingHeading() { | ||
| if (timeRemainingHeading === null) { | ||
| timeRemainingHeading = document.getElementById("timeRemaining"); | ||
| } | ||
|
|
||
| return timeRemainingHeading; | ||
| } | ||
|
|
||
| function formatTime(totalSeconds) { | ||
| // Convert total seconds to minutes and seconds | ||
| const minutes = Math.floor(totalSeconds / 60); | ||
| const seconds = totalSeconds % 60; | ||
|
|
||
| return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; | ||
| } | ||
|
|
||
| function updateHeading(totalSeconds) { | ||
| // Update the heading with the formatted time remaining | ||
| const heading = getTimeRemainingHeading(); // Query once, then reuse the cached DOM node on each tick | ||
| heading.innerText = `Time Remaining: ${formatTime(totalSeconds)}`; // Set the text of the heading to show the time remaining in the format "Time Remaining: MM:SS" | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); // Get the input element using its DOM ID | ||
| let remainingSeconds = Number(input.value); // Convert the input value to a number representing the total seconds for the countdown | ||
|
|
||
| // Guard against edge cases: empty input, non-numeric, negative values, and 0 | ||
| if (!Number.isFinite(remainingSeconds) || remainingSeconds <= 0) { | ||
| // Check if the input is a valid number and strictly greater than 0 | ||
| alert("Please enter a positive number greater than 0"); | ||
| return; // Exit early without starting the countdown | ||
| } | ||
|
|
||
| if (countdownId !== null) { | ||
| // If there is an existing countdown, clear it before starting a new one | ||
|
|
||
| clearInterval(countdownId); | ||
| } | ||
|
|
||
|
|
||
| // Reset background and stop alarm sound when setting a new alarm | ||
| document.body.style.backgroundColor = originalBackgroundColor; | ||
|
|
||
| updateHeading(remainingSeconds); // Update the heading to show the initial time remaining before starting the countdown | ||
|
|
||
| countdownId = setInterval(() => { | ||
| // Start a new interval that will execute the provided function every 1000 milliseconds (1 second) | ||
| remainingSeconds -= 1; | ||
|
|
||
| if (remainingSeconds <= 0) { | ||
| // If the remaining seconds reach 0 or below, stop the countdown and play the alarm | ||
| updateHeading(0); | ||
| clearInterval(countdownId); | ||
| countdownId = null; | ||
| playAlarm(); | ||
| document.body.style.backgroundColor = "red"; // change background colour | ||
|
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. This is an awesome touch, minor point is that when the background becomes red, then a new timer could reset it back to the original background (until it's done counting and it goes back to red). How can we implement that?
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. Reset screen to white when timer set. |
||
|
|
||
| return; // Exit the function to prevent further execution | ||
| } | ||
|
|
||
| updateHeading(remainingSeconds); | ||
| }, 1000); | ||
| } | ||
|
|
||
| // 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.
Completely optional and can be personal preference but we might have over-commented some of the lines, some comments are genuinely useful and are great to document code but others might be consider self explanatory.
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.
I think you're right about style- I have put most of the comments in as a reminder to myself when I look at it again- not very neat and collaborative, but useful for me while I'm learning and possibly reusing in future modules. If there's a better or more standard way of doing that, I'm happy to comply? Thanks
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.
Hey @DamianDL , typically when we raise PRs, we would only keep comments for things that really aren't obvious from the code alone, like context or complicated implementation that would require that extra info (ideally the code self-documents what we are trying to do with proper variable and function names), but for learning purposes I don't feel too strongly against keeping them.
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