Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
55 changes: 54 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
function setAlarm() {}
let timer;

function updateDisplay(timeRemaining, heading) {
let minutes = Math.floor(timeRemaining / 60);
let seconds = timeRemaining % 60;

heading.innerText =
"Time Remaining: " +
String(minutes).padStart(2, "0") +
":" +
String(seconds).padStart(2, "0");
}

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

let timeRemaining = parseInt(input.value);

if (isNaN(timeRemaining) || timeRemaining <= 0) {
alert("Please enter a positive number of seconds.");
return;
}

clearInterval(timer);
pauseAlarm();

audio.currentTime = 0;
document.body.style.backgroundColor = "";

updateDisplay(timeRemaining, heading);

function countdown() {
timeRemaining = timeRemaining - 1;

updateDisplay(timeRemaining, heading);

if (timeRemaining <= 0) {
clearInterval(timer);
document.body.style.backgroundColor = "red";
playAlarm();
}
}

timer = setInterval(countdown, 1000);
}

function stopAlarm() {
clearInterval(timer);
pauseAlarm();
}

document.getElementById("set").addEventListener("click", setAlarm);
document.getElementById("stop").addEventListener("click", stopAlarm);

@cjyuan cjyuan Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The original code also assigns event listeners (on lines 61-67) to these two buttons. Could you find a way to prevent setAlarm() and pauseAlarm() from being called twice (without modifying the original code)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I prevented the duplicate calls by adding event listeners in the capture phase and using event.stopImmediatePropagation(). This stops the original event listeners from firing as well, so setAlarm() and pauseAlarm() are each called only once, without modifying the original code below the DO NOT EDIT section.
I hope this resolves the issue and that you can now mark the assignment as complete.


// 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
Loading