-
-
Notifications
You must be signed in to change notification settings - Fork 283
Glasgow| 2026-ITP-Jan | Tuan Nguyen| Sprint 3 | Alarm clock #1209
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 6 commits
d77934a
c5122f9
832f671
005369b
0f8a136
a04f01c
0bae369
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,5 +1,60 @@ | ||
| function setAlarm() {} | ||
| let countdown = null; | ||
|
|
||
| function resetAlarm() { | ||
| clearInterval(countdown); | ||
| countdown = null; | ||
| pauseAlarm(); | ||
| updateHeading(0); | ||
| } | ||
|
|
||
| function showError(message) { | ||
| const heading = document.getElementById("timeRemaining"); | ||
| heading.innerText = message; | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
| let time = Number(input.value); | ||
|
Contributor
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. What type and range of number should
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 the feed back I have add validation cod to help with this here is my validation code below: if (isNaN(time) || time < 0) {
Contributor
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. What do you expect the app to behave when the user enters
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 the feed back here is my explain when decimal is pass down: If the user enters 3.14, Number() converts it to a floating‑point number. const minutes = Math.floor(time / 60); This behaviour is technically valid but not intuitive for users, since countdown timers usually expect whole seconds. A more predictable behaviour would be to either: reject decimal input as invalid, or convert it to an integer using Math.floor() or Math.round(). This would make the app’s behaviour clearer and more consistent. Thank you for the reading and I will some change base on this explanation to make sure the function work properly.
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. In general if you're just ignoring user input (as you do if someone puts in a float), it's good to show an error message telling them what's wrong, rather than just ignore it. Also, if you already had an alarm counting down, then submitted 1.2, it will set to 00:00 but then shortly after start counting down again. Can you fix up both of these things?
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. Hello @illicitonion thank you for your feed back I have understand what your like to ask me to do so here is my validation code that will show on the HTML page: if (isNaN(time) || time < 0 || !Number.isInteger(time)) { |
||
|
|
||
| resetAlarm(); | ||
|
|
||
| if (isNaN(time) || time < 0 || !Number.isInteger(time)) { | ||
| showError("Please enter a whole number of seconds."); | ||
| return; | ||
| } | ||
|
|
||
| if (time === 0) { | ||
| updateHeading(0); | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| updateHeading(time); | ||
|
|
||
| countdown = setInterval(() => { | ||
| time--; | ||
|
|
||
| if (time <= 0) { | ||
| updateHeading(0); | ||
| clearInterval(countdown); | ||
| playAlarm(); | ||
| } else { | ||
| updateHeading(time); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| function updateHeading(time) { | ||
| const heading = document.getElementById("timeRemaining"); | ||
|
|
||
| const minutes = Math.floor(time / 60); | ||
| const seconds = time % 60; | ||
|
|
||
| const mm = String(minutes).padStart(2, "0"); | ||
| const ss = String(seconds).padStart(2, "0"); | ||
|
|
||
| heading.innerText = `Time Remaining: ${mm}:${ss}`; | ||
| } | ||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
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.
Would be better to introducing a separate element to showing message.
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.
@cjyuan thank you for you reply I have making change that now will display the error without interfering with the heading element in HTML page. Here is my solution:
function showError(message) {
const heading = document.getElementById("errorMessage");
heading.innerText = message;
}
function setAlarm() {
const input = document.getElementById("alarmSet");
let time = Number(input.value);
resetAlarm();
if (isNaN(time) || time < 0 || !Number.isInteger(time)) {
showError("Please enter a whole number of seconds.");
return;
}
document.getElementById("errorMessage").innerText = "";
for better checking I have make an commit with full code so you can check for better view of the whole code. Thank you.