Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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() {
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, 1000);
Copy link

Choose a reason for hiding this comment

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

here 1000 have meaning and these are called magic numbers, explore a bit on handling magic numbers in your code. Would it be more readable if you extract this? How would you do it?

Copy link
Author

Choose a reason for hiding this comment

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

I have implemented these fixes, I have also reviewed magic numbers for this exercise and decided to use a constant instead, please review.

Copy link

Choose a reason for hiding this comment

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

yes, I can see your pushed the fixes for minutes and second, however I cannot see the constant on this file for 1000!

Copy link
Author

Choose a reason for hiding this comment

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

Apologies I added it to the wrong sprint I have now reverted that file and committed it now, please review.

}

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