Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
function setAlarm() {}
let countdown = null;

function resetAlarm() {
clearInterval(countdown);
countdown = null;
pauseAlarm();
updateHeading(0);
}

function setAlarm() {
const input = document.getElementById("alarmSet");
let time = Number(input.value);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What type and range of number should time be? Invalid user input could cause the app to behave abnormally.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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) {
updateHeading(0);
return;
}

Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you expect the app to behave when the user enters 3.14 as input?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.
The countdown will decrease by 1 each second (3.14 → 2.14 → 1.14 → 0.14 → alarm), but the display will show whole seconds because of the formatting.

const minutes = Math.floor(time / 60);
const seconds = 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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)) {
showError("Please enter a whole number of seconds.");
return;
}


if (isNaN(time) || time < 0 || !Number.isInteger(time)) {
updateHeading(0);
return;
}

resetAlarm();

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");
Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm clock app</title>
</head>
<body>
<div class="centre">
Expand Down
Loading