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

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 `${m}:${s}`;
}

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

function tick() {

Choose a reason for hiding this comment

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

Good job! @enjoy15

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;

Choose a reason for hiding this comment

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

Nice validation! @enjoy15
However, it would help readability to add a short comment explaining what this guard clause does.

Copy link
Author

Choose a reason for hiding this comment

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

comment added

if (intervalId) clearInterval(intervalId);
remaining = value;
updateHeading();
intervalId = setInterval(tick, 1000);
}

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 app</title>
</head>
<body>
<div class="centre">
Expand Down