Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
47 changes: 46 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
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 minutes = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${minutes}m : ${secs}s`;
}


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

<label for="alarmSet">Set time in seconds:</label>
<input
id="alarmSet"
type="number"
placeholder="Enter time in seconds"
/>

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

<script src="alarmclock.js"></script>
</body>
</html>
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"
}
}