-
-
Notifications
You must be signed in to change notification settings - Fork 283
Sheffield | 26-ITP-Jan | Mona-Eltantawy | Sprint 3| Alarm clock #1158
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 5 commits
d598894
3eaa8c0
a204333
384a4dd
7210f43
83beeba
0e2fdf2
60ea681
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,17 +1,48 @@ | ||
| function setAlarm() {} | ||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
| let timeRemaining = 0; | ||
| let countdownInterval = null; | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet").value; | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| }); | ||
| timeRemaining = parseInt(input, 10); | ||
|
|
||
| if (isNaN(timeRemaining) || timeRemaining <= 0) { | ||
| alert("Please enter a valid number greater than 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. Nice error handling. It's important to validate user input
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. I've changed the alert to 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. I did not intent to request a change here. It was already good. The advantage of the alert is that it is more visible to the user. For the first exercises I think alerts and console.error logs are both fine. |
||
| return; | ||
| } | ||
|
|
||
| updateDisplay(); | ||
|
|
||
| if (countdownInterval) { | ||
| clearInterval(countdownInterval); | ||
| } | ||
|
|
||
| countdownInterval = setInterval(() => { | ||
| timeRemaining--; | ||
|
|
||
| updateDisplay(); | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| }); | ||
| if (timeRemaining <= 0) { | ||
| clearInterval(countdownInterval); | ||
| playAlarm(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| function updateDisplay() { | ||
| const display = document.getElementById("timeRemaining"); | ||
|
|
||
| const minutes = String(Math.floor(timeRemaining / 60)).padStart(2, "0"); | ||
| const seconds = String(timeRemaining % 60).padStart(2, "0"); | ||
|
|
||
| display.textContent = `Time Remaining: ${minutes}:${seconds}`; | ||
| } | ||
|
|
||
| function setup() { | ||
| document.getElementById("set").addEventListener("click", setAlarm); | ||
| document.getElementById("stop").addEventListener("click", pauseAlarm); | ||
| } | ||
|
|
||
| function playAlarm() { | ||
|
|
@@ -20,6 +51,11 @@ function playAlarm() { | |
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| audio.currentTime = 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. Nice that you reset the audio time here |
||
|
|
||
| if (countdownInterval) { | ||
| clearInterval(countdownInterval); | ||
| } | ||
| } | ||
|
|
||
| window.onload = setup; | ||
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 your code should be above this line
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 changed the whole page multiple times so I don't have the line now