-
-
Notifications
You must be signed in to change notification settings - Fork 327
Cape Town| 26-ITP-May | Liyema Mfengwana| Sprint 3 | Alarm clock app #1417
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
93a73e6
0cf81a3
af2f111
18dfc07
b9d984c
15bfe59
d78233b
cc49374
3e316db
ee991a5
eb7342b
8d40548
8ace4a4
21be02b
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,56 @@ | ||
| function setAlarm() {} | ||
| let timer; // let because timer will be assigned a new timeout ID each time. | ||
| let timerRunning = false; | ||
|
|
||
| const input = document.getElementById("alarmSet"); | ||
|
|
||
| input.addEventListener("keydown", function (event) { | ||
| if (event.key === "-" || event.key === "e") { | ||
| event.preventDefault(); | ||
| } | ||
| }); | ||
|
|
||
| function setAlarm() { | ||
| if (timerRunning) { | ||
| return; | ||
| } | ||
|
|
||
| const input = document.getElementById("alarmSet"); | ||
|
|
||
| // Convert the input value from a string to a number | ||
| let seconds = Number(input.value); | ||
|
|
||
| // Don't start the alarm if the input is empty or negative/zero | ||
| if (input.value === "" || seconds <= 0) { | ||
| return; | ||
| } | ||
|
|
||
| timerRunning = true; | ||
| document.getElementById("set").disabled = true; | ||
|
|
||
| function countdown() { | ||
| const minutes = Math.floor(seconds / 60); | ||
| const remainingSeconds = seconds % 60; | ||
|
|
||
| const formattedMinutes = String(minutes).padStart(2, "0"); | ||
| const formattedSeconds = String(remainingSeconds).padStart(2, "0"); | ||
|
|
||
| document.getElementById("timeRemaining").textContent = | ||
| `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; | ||
|
|
||
| if (seconds === 0) { | ||
| playAlarm(); | ||
| input.value = ""; | ||
| timerRunning = false; | ||
| document.getElementById("set").disabled = false; | ||
| return; | ||
| } | ||
|
|
||
| seconds--; | ||
| timer = setTimeout(countdown, 1000); | ||
| } | ||
|
|
||
| countdown(); | ||
|
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. Try in the browser: set the alarm to 30, then straight away change the input to 10 and click Set Alarm again. What do you expect to happen? What actually happens? Think about how many countdowns are running at that point, and what each one is doing to the heading.
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. Thanks for the feedback! I've made the requested changes: Added validation for empty and non-positive input. |
||
| } | ||
|
|
||
| // 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.
2 cases to check
Number("")give you?type="number", so a user can type-5. Try it pls.