-
-
Notifications
You must be signed in to change notification settings - Fork 195
NW| 25-ITP-Sep | Ahmad Hmedan | Sprint 3 | Alarm clock #911
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 4 commits
229a2de
ee63ed7
b4bde21
ccf9c34
2cd628f
61111e7
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,41 @@ | ||
| function setAlarm() {} | ||
| let timerId = null; | ||
| let remainingSeconds = 0; | ||
|
|
||
| function updateDisplay() { | ||
|
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 separation of display logic |
||
| let minutes = String(Math.floor(remainingSeconds / 60)).padStart(2, "0"); | ||
|
||
| let seconds = String(remainingSeconds % 60).padStart(2, "0"); | ||
|
||
| document.getElementById( | ||
| "timeRemaining" | ||
| ).textContent = `Time Remaining: ${minutes}:${seconds}`; | ||
| } | ||
|
|
||
| function startCountdown() { | ||
|
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 function name — clear purpose. |
||
| if (remainingSeconds < 0) remainingSeconds = 0; | ||
| timerId = setInterval(() => { | ||
| updateDisplay(); | ||
|
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. This runs before decrementing remainingSeconds, causing the countdown to visually lag by one second. |
||
|
|
||
| if (remainingSeconds <= 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 sequence of logic in this if block would cause some issues.
Why not Subtract first, then check? |
||
| remainingSeconds = 0; | ||
| clearInterval(timerId); | ||
| timerId = null; | ||
| playAlarm(); | ||
| } | ||
| remainingSeconds -= 1; | ||
| }, 1000); | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
|
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 think we can have better validation in setAlarm function.
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 for your feedback — it was really helpful, and I understood what you meant straight away.
Member
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. Within a frontend, JavaScript can generally rely on HTML validation. But generally when crossing system boundaries (e.g. if your frontend was posting data to a backend), each separate component needs to decide whether it trusts whoever is sending data to it, or whether its should validate it (again). In general, though, HTML and JavaScript in a browser can be treated like one component which can trust each other for this kind of thing. But e.g. your HTML has no validation that the number is > 0, so it's good that you validate that here. (It's an interesting question whether to treat |
||
| if (input.value === "") return; | ||
| const inputValue = Number(input.value); | ||
| clearInterval(timerId); | ||
| timerId = null; | ||
| remainingSeconds = inputValue; | ||
|
|
||
| updateDisplay(); | ||
| startCountdown(); | ||
| input.value = ""; | ||
| } | ||
|
|
||
| // 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.
✔ Good: clear global state variables.