-
-
Notifications
You must be signed in to change notification settings - Fork 191
West Midlands | 25 Sep ITP | Iswat Bello | Sprint 3 | Build alarm clock app #865
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
0a669a9
4131d89
37c1d95
51e3ab2
eb4d2b8
c9e5625
06816bc
57749a1
748f887
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,67 @@ | ||
| function setAlarm() {} | ||
| // Declare a global variable to store the countdown currently running | ||
| let intervalId; | ||
| const timesRemaining = "Time Remaining:"; | ||
|
|
||
| // Helper function to format total seconds and update the UI | ||
| function updateDisplay(totalSeconds) { | ||
| let timeRemainingMinutes = Math.floor(totalSeconds / 60) | ||
| .toString() | ||
| .padStart(2, "0"); | ||
| let timeRemainingSeconds = (totalSeconds % 60).toString().padStart(2, "0"); | ||
|
|
||
| document.getElementById( | ||
| "timeRemaining" | ||
| ).textContent = `${timesRemaining} ${timeRemainingMinutes}:${timeRemainingSeconds}`; | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| if (intervalId) { | ||
| clearInterval(intervalId); // when the set alarm button is clicked, if there is a countdown running, clear it | ||
| } | ||
| // When I press the “Set Alarm” button: | ||
| // Read the number typed in the input field (in seconds) | ||
| // Store this value in a variable called totalSeconds | ||
| // Convert totalSeconds into minutes and seconds and save them inside timeRemainingMinutes and timeRemainingSeconds respectively | ||
| // Format timeRemainingMinutes and timeRemainingSeconds so they always have 2 digits (mm:ss) | ||
| // Display "Time Remaining: mm:ss" on the UI | ||
| // Start a repeating timer using setInterval that runs every 1 second: | ||
| // Decrease timeRemaining by 1 | ||
| // Convert the new time into mm:ss | ||
| // Update the display | ||
| // If timeRemaining reaches 0: | ||
| // Stop the interval | ||
| // Display "00:00" | ||
| // Call playAlarm() | ||
|
|
||
| let inputElement = document.getElementById("alarmSet"); // this is used to grab the input element so I can read whatever the user typed | ||
| let totalSeconds = Number(inputElement.value); //this converts the input from string to number | ||
|
|
||
| // Validate input | ||
| if (totalSeconds <= 0 || isNaN(totalSeconds)) { | ||
|
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. Good validation!
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. Thank you! |
||
| alert("Please enter a positive number!"); | ||
| return; | ||
| } | ||
|
|
||
| document.getElementById("timeRemaining").style.backgroundColor = ""; | ||
|
|
||
| updateDisplay(totalSeconds); | ||
| // start the countdown | ||
| intervalId = setInterval(() => { | ||
| // the code you write here will run every one second or 1000 milliseconds | ||
| totalSeconds = totalSeconds - 1; | ||
| if (totalSeconds <= 0) { | ||
| clearInterval(intervalId); | ||
|
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. Correct use of
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. Thank you |
||
| playAlarm(); | ||
| document.getElementById("timeRemaining").textContent = | ||
| `${timesRemaining} 00:00`; | ||
| document.getElementById("timeRemaining").style.backgroundColor = "blue"; | ||
| document.getElementById("alarmSet").value = ""; | ||
| return; | ||
| } | ||
|
|
||
| updateDisplay(totalSeconds); | ||
| }, 1000); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "devDependencies": { | ||
| "@testing-library/jest-dom": "^6.9.1", | ||
| "jest-environment-jsdom": "^30.2.0" | ||
| } | ||
| } |
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.
Nice use of a helper function @Iswanna
Separating the display formatting into its own function is excellent practice and keeps setAlarm() cleaner.
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.
Thank you for the feedback @jaymes15.