-
-
Notifications
You must be signed in to change notification settings - Fork 320
London | 26-ITP-May | Zadri Abdule | Sprint 3 | alarm-clock app #1319
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,45 @@ | ||
| function setAlarm() {} | ||
| function setAlarm(){ | ||
| const input = document.getElementById("alarmSet"); | ||
| const heading = document.getElementById("timeRemaining"); | ||
|
|
||
| let seconds = parseInt(input.value, 10); | ||
| if (!Number.isFinite(seconds) || seconds < 0) seconds = 0; | ||
|
|
||
| function formatMMSS(s) { | ||
|
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 job putting the code for formatting into an own function. This makes it reusable and easier to change the formtting if needed |
||
| const mm = Math.floor(s / 60) | ||
| .toString() | ||
| .padStart(2, "0"); | ||
| const ss = (s % 60).toString().padStart(2, "0"); | ||
| return `Time Remaining: ${mm}:${ss}`; | ||
| } | ||
|
|
||
| heading.innerText = formatMMSS(seconds); | ||
|
|
||
| if (window._alarmIntervalId) { | ||
|
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. Why are you adding |
||
| clearInterval(window._alarmIntervalId); | ||
| window._alarmIntervalId = null; | ||
| } | ||
|
|
||
| // If it's already zero, play immediately and don't start an interval | ||
| if (seconds === 0) { | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| window._alarmIntervalId = setInterval(() => { | ||
| seconds -= 1; | ||
|
|
||
| if (seconds > 0) { | ||
| heading.innerText = formatMMSS(seconds); | ||
| } else { | ||
| // Ensure the heading shows 00:00, stop the interval and play the alarm once | ||
| heading.innerText = formatMMSS(0); | ||
| clearInterval(window._alarmIntervalId); | ||
| window._alarmIntervalId = null; | ||
| playAlarm(); | ||
| } | ||
| }, 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.
I am not sure if the expected user behaviour for invalid inputs is to set the seconds to 0. What else could you do instead?