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
35 changes: 34 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
function setAlarm() {}
let intervalId = null;

function formatTime(totalSeconds) {
const mins = Math.floor(totalSeconds / 60);
const secs = totalSeconds % 60;
return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
}

function setAlarm() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
}

const input = document.getElementById("alarmSet");
let remaining = Number(input.value);

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 happens to the countdown when Set Alarm is clicked with the number field empty?

To "like" or "dislike" this comment, please follow this link

const heading = document.getElementById("timeRemaining");

heading.textContent = `Time Remaining: ${formatTime(remaining)}`;
input.value = "";

intervalId = setInterval(() => {
remaining -= 1;
heading.textContent = `Time Remaining: ${formatTime(remaining)}`;

if (remaining <= 0) {
clearInterval(intervalId);
intervalId = null;
playAlarm();
document.body.style.backgroundColor = "red";
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand All @@ -11,6 +43,7 @@ function setup() {

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
document.body.style.backgroundColor = "white";
});
}

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
Loading