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
59 changes: 57 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What will happen if we add 1.5 in the timer? How can we fix it?

Copy link
Copy Markdown
Author

@lauracs24 lauracs24 Mar 27, 2026

Choose a reason for hiding this comment

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

Sorry I missed this!

I’ve now updated the implementation to ensure the timer only uses whole seconds by converting the input to an integer and added validation to handle invalid inputs @hkavalikas

Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
function setAlarm() {}
let secondsRemaining = 0;
let timer = null;

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

const value = Number(input.value);

if (!Number.isFinite(value) || value <= 0) {
updateHeading(heading, 0);
return;
}

secondsRemaining = Math.floor(value);

if (timer !== null) {
clearInterval(timer);
timer = null;
}

updateHeading(heading, secondsRemaining);

timer = setInterval(() => {
secondsRemaining -= 1;

updateHeading(heading, secondsRemaining);

if (secondsRemaining === 0) {
clearInterval(timer);
timer = null;
playAlarm();
}
}, 1000);
}

function updateHeading(heading, totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

const formattedMinutes = String(minutes).padStart(2, "0");
const formattedSeconds = String(seconds).padStart(2, "0");

heading.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}

function stopTimer() {
if (timer !== null) {
clearInterval(timer);
timer = null;
}
}

window.addEventListener("load", () => {
document.getElementById("stop").addEventListener("click", stopTimer);
});

// DO NOT EDIT BELOW HERE

Expand All @@ -22,4 +77,4 @@ function pauseAlarm() {
audio.pause();
}

window.onload = setup;
window.onload = setup;
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
5 changes: 4 additions & 1 deletion Sprint-3/alarmclock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"bugs": {
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"@testing-library/jest-dom": "^6.9.1"
}
}
Loading