Skip to content
Closed
53 changes: 52 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
function setAlarm() {}
// Constants for time conversion
const SECONDS_PER_MINUTE = 60;
const MILLISECONDS_PER_SECOND = 1000;

let intervalId;

/**
* Formats time in seconds to MM:SS format
* @param {number} totalSeconds - The time in seconds to format
* @returns {string} Formatted time string in "Time Remaining: MM:SS" format
*/
function formatTimeDisplay(totalSeconds) {
const minutes = Math.floor(totalSeconds / SECONDS_PER_MINUTE);
const seconds = totalSeconds % SECONDS_PER_MINUTE;

return (
"Time Remaining: " +
String(minutes).padStart(2, "0") +
":" +
String(seconds).padStart(2, "0")
);
}

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

let totalSeconds = Number(input.value);

// Input validation: check for invalid or non-positive numbers
if (isNaN(totalSeconds) || totalSeconds <= 0) {
heading.innerText = "Please enter a valid positive number of seconds";
return;
}
Comment on lines +24 to +51
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.

Some input that can mess up the clock display can still pass this check.


clearInterval(intervalId);
Comment thread
cjyuan marked this conversation as resolved.
Comment on lines +53 to +54
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.

Could consider placing all the "resetting" code together (code on lines 54 and 28).


heading.innerText = formatTimeDisplay(totalSeconds);

intervalId = setInterval(() => {
totalSeconds--;

if (totalSeconds <= 0) {
heading.innerText = formatTimeDisplay(0);
clearInterval(intervalId);
playAlarm();
return;
}

heading.innerText = formatTimeDisplay(totalSeconds);
}, MILLISECONDS_PER_SECOND);
}

// DO NOT EDIT BELOW HERE

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
2 changes: 1 addition & 1 deletion Sprint-3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/user-event": "^14.6.1",
"jest": "^30.0.4",
"jest": "^30.2.0",
"jest-environment-jsdom": "^30.0.4"
}
}