-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Anita Amirhaeri | Sprint 3 | Alarm Clock #1388
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
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,53 @@ | ||
| function setAlarm() {} | ||
| const input = document.getElementById("alarmSet"); | ||
| const timeDisplay = document.querySelector("span"); | ||
|
|
||
| let alarmInterval = null; | ||
|
|
||
| function setAlarm() { | ||
| // sanitize input: convert to number, floor to integer seconds, clamp to >= 0 | ||
| let timeInput = Math.floor(Number(input.value) || 0); | ||
| if (timeInput < 0) timeInput = 0; | ||
|
|
||
| // clear any existing interval so multiple clicks don't create multiple timers | ||
| if (alarmInterval) { | ||
| clearInterval(alarmInterval); | ||
| alarmInterval = null; | ||
| } | ||
|
|
||
| // handle negative input explicitly | ||
| const rawValue = Number(input.value); | ||
| if (rawValue < 0) { | ||
|
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. Since we've already type casted and validated the input above is there a need to do this here again? |
||
| timeDisplay.textContent = "Invalid value"; | ||
| return; | ||
| } | ||
|
|
||
| let minutes = Math.floor(timeInput / 60); | ||
| let seconds = timeInput % 60; | ||
|
|
||
| minutes = minutes.toString().padStart(2, "0"); | ||
|
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. the formatting logic is duplicated, can we extract out this logic into a function to remove code duplication? |
||
| seconds = seconds.toString().padStart(2, "0"); | ||
| timeDisplay.textContent = `${minutes}:${seconds}`; | ||
|
|
||
| // if the input is 0, trigger the alarm immediately | ||
| if (timeInput <= 0) { | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| alarmInterval = setInterval(() => { | ||
| timeInput--; | ||
| let remainingMinutes = Math.floor(timeInput / 60); | ||
| let remainingSeconds = timeInput % 60; | ||
| remainingMinutes = remainingMinutes.toString().padStart(2, "0"); | ||
| remainingSeconds = remainingSeconds.toString().padStart(2, "0"); | ||
| timeDisplay.textContent = `${remainingMinutes}:${remainingSeconds}`; | ||
| if (timeInput <= 0) { | ||
| playAlarm(); | ||
| clearInterval(alarmInterval); | ||
| alarmInterval = null; | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
@@ -20,6 +69,11 @@ function playAlarm() { | |
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| audio.currentTime = 0; | ||
| if (alarmInterval) { | ||
| clearInterval(alarmInterval); | ||
| alarmInterval = null; | ||
| } | ||
| } | ||
|
|
||
| window.onload = setup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,19 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <title>Title here</title> | ||
| </head> | ||
| <body> | ||
| <div class="centre"> | ||
| <h1 id="timeRemaining">Time Remaining: 00:00</h1> | ||
| <label for="alarmSet">Set time to:</label> | ||
| <input id="alarmSet" type="number" /> | ||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| </div> | ||
| <script src="alarmclock.js"></script> | ||
| </body> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <link rel="stylesheet" href="style.css" /> | ||
| <title>Alarm clock app</title> | ||
| </head> | ||
| <body> | ||
| <div class="centre"> | ||
| <h1 id="timeRemaining">Time Remaining: <span>00:00</span></h1> | ||
| <label for="alarmSet">Set time to:</label> | ||
| <input id="alarmSet" type="number" min="1" /> | ||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| </div> | ||
| <script src="alarmclock.js"></script> | ||
| </body> | ||
| </html> |
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 would happen If I set the alarm for unexpected values such as an empty string or 0?