generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 220
Birmingham | ITP-Sep-2025 |Ahmad Ehsas | Sprint 3 | Alarm Clock #864
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0aa234b
Created the updateDisplay function to update the screen Time Remainin…
ahmadehsas c0ed942
Executed the setAlarm function that we can read the number the user t…
ahmadehsas ec2a9ff
Reset the alarm to make sure next time starts from the beginning and …
ahmadehsas 83bf7f8
Made the background color change when the alarm clock finishes and re…
ahmadehsas b094924
removed 'input is invalid' from the return on line 9 and moved the le…
ahmadehsas 3d400c6
When the set Alarm button is clicked, the backround color reset to it…
ahmadehsas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,54 @@ | ||
| function setAlarm() {} | ||
| let countdown = null; | ||
| function setAlarm() { | ||
| document.body.style.backgroundColor = ""; // reset the background color when set Alarm button is clicked | ||
| const input = document.getElementById("alarmSet") // get the input field | ||
| let timeLeft = Number(input.value); // convert input value to a number | ||
|
|
||
| // validate the input value | ||
| if (!Number.isFinite(timeLeft) || timeLeft <= 0) { // The Number.isFinite() method is a way to test whether a value | ||
| // is a finite number value. and also prevents the error if the user types nothing or a negative number. | ||
| return; // input is invalid | ||
| } | ||
|
|
||
| updateDisplay(timeLeft); // show the starting time immediately | ||
|
|
||
| if (countdown) { | ||
| clearInterval(countdown) | ||
| // clear previous countdown if running | ||
| // if a countdown is already running stop it before starting a new one. | ||
| } | ||
|
|
||
| // start countdown | ||
| countdown = setInterval(() => { | ||
| timeLeft -= 1; // reduce time by 1 seconds | ||
| updateDisplay(timeLeft); // update display | ||
|
|
||
| if (timeLeft <= 0) { // when time reaches 0 | ||
| clearInterval(countdown); // stop the countdown | ||
| countdown = null; // reset variable | ||
| playAlarm(); // play the alarm sound | ||
|
|
||
| // change background color when the alarm clock finishes | ||
| document.body.style.backgroundColor = "red"; | ||
| } | ||
|
|
||
| }, 1000); // run every one second | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| function updateDisplay(totalSeconds) { | ||
| console.log("updating display with", totalSeconds); | ||
| const display = document.getElementById("timeRemaining"); // find h1 element | ||
|
|
||
| if (totalSeconds < 0) totalSeconds = 0; // safety prevent negative time. | ||
|
|
||
| const mins = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); // Math.floor method rounds down and return the larges integer less than or equal to a given number. | ||
| const secs = String(totalSeconds % 60).padStart(2, "0"); // calculate remaining seconds. | ||
|
|
||
| display.textContent = `Time Remaining: ${mins}:${secs}`; // update h1 text | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -20,6 +70,18 @@ function playAlarm() { | |
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| audio.currentTime = 0; // reset to start, make sure next time play alarm starts from the beginning. | ||
|
|
||
| // reset the background color to original | ||
| document.body.style.backgroundColor = ""; | ||
|
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 good! |
||
|
|
||
| if(countdown) { | ||
| clearInterval(countdown); // clearing the interval stops the countdown immediately if user press Stop alarm | ||
| countdown = null; | ||
| } | ||
| } | ||
|
|
||
| window.onload = setup; | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What happens when the user lets the alarm finish (without clicking stop), the background stays red forever.
I think you should reset he background color before starting a new alarm, inside
setAlarm()