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
46 changes: 45 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
function setAlarm() {}
const heading = document.getElementById("timeRemaining");
const input = document.getElementById("alarmSet");
const setBtn = document.getElementById("set");

const ONE_SECOND = 1000;

let remaining = 0;
let intervalId = null;

function format(seconds) {
const minute = String(Math.floor(seconds / 60)).padStart(2, "0");
const second = String(seconds % 60).padStart(2, "0");
return `${minute}:${second}`;
Copy link

@JaypeeLan JaypeeLan Nov 24, 2025

Choose a reason for hiding this comment

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

I think the timer could be more descriptive. Something like 10m : 22s , because the user is just putting numbers in seconds, and you do the conversion in the background. Then in your HTML input field, use a placeholder to specify the number being inputted, should be in seconds.

}

function updateHeading() {
heading.textContent = `Time Remaining: ${format(remaining)}`;
}

function tick() {
if (remaining <= 0) {
clearInterval(intervalId);
intervalId = null;
updateHeading();
if (typeof window.playAlarm === "function") {
window.playAlarm();
}
return;
}
remaining -= 1;
updateHeading();
}

function setAlarm() {
const value = parseInt(input.value, 10);
// Guard clause: ignore invalid or non-positive input
if (!Number.isFinite(value) || value <= 0) return;
if (intervalId) clearInterval(intervalId);
remaining = value;
updateHeading();
intervalId = setInterval(tick, ONE_SECOND);
}

setBtn.addEventListener("click", setAlarm);
updateHeading();

// 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</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"
}
}