Skip to content
50 changes: 49 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
function setAlarm() {}
let intervalId = null;

function formatTime(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

const formattedMinutes = String(minutes).padStart(2, "0");
const formattedSeconds = String(seconds).padStart(2, "0");

return `${formattedMinutes}:${formattedSeconds}`;
}

function setAlarm() {
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

let value = input.value

if (value === "") {
heading.innerText = "Please enter a number of seconds.";
return;

Choose a reason for hiding this comment

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

Instead of returning it will be better to show the user an appropriate message, can you think of a way to do that please.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @A-O-Emmanuel, thank you for the review comments. I have updated the validation so that if the input field is empty, the app now displays a user message ("Please enter a number of seconds.") instead of returning silently. This prevents the timer from starting without valid input.

}

let remainingSeconds = Number(value);

if (remainingSeconds <= 0 || isNaN(remainingSeconds)) {
heading.innerText = "Please enter a positive number greater than 0.";
return;

Choose a reason for hiding this comment

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

Also instead of returning, it will be better you show the user an appropriate message, think of a way to do that.

Copy link
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 review comment. I have now updated the logic so that if the value is invalid (NaN) or less than or equal to zero, the app displays the message ("Please enter a positive number greater than 0.") rather than returning silently.

}

heading.innerText = `Time Remaining: ${formatTime(remainingSeconds)}`;

if (intervalId !== null) {
clearInterval(intervalId);
}

intervalId = setInterval(() => {
remainingSeconds -= 1;

if (remainingSeconds <= 0) {
heading.innerText = `Time Remaining: ${formatTime(0)}`;
playAlarm();
clearInterval(intervalId);
intervalId = null;
return;
}
heading.innerText = `Time Remaining: ${formatTime(remainingSeconds)}`;
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />
<input id="alarmSet" type="number" min="1" step="1"/>

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
Expand Down