Skip to content
Open
48 changes: 43 additions & 5 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
function setAlarm() {}
let countdown;
Comment thread
cjyuan marked this conversation as resolved.
Outdated

// reset before starting new countdown
function resetAlarm() {
clearInterval(countdown);
audio.pause();
document.getElementById("timeRemaining").textContent = "Time Remaining: 00:00";
document.body.classList.toggle("alarm-activated", false);
}
Comment thread
cjyuan marked this conversation as resolved.
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 16, 2026

Choose a reason for hiding this comment

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

Shouldn't this function, resetAlarm() be called in setAlarm()?

You should also reset the audio because the user may not click the Stop button before starting a new countdown.

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.

Implemented. Thank you for your time!


function setAlarm() {
let seconds = parseInt(document.getElementById("alarmSet").value);

if (!seconds || seconds < 1) {
alert("The number of seconds must be more than 0 please");
return;
}
function updateDisplay() {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
document.getElementById("timeRemaining").textContent =
`Time Remaining: ${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`;
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated

updateDisplay(); // update immediately on click

// code to reset background
function pauseAlarm() {
audio.pause();
document.body.classList.toggle("alarm-activated", false);
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated

countdown = setInterval(() => {
seconds--;
updateDisplay();

if (seconds <= 0) {
clearInterval(countdown);
playAlarm();
document.body.classList.toggle("alarm-activated", true);
}
}, 1000);
}

// DO NOT EDIT BELOW HERE
Comment thread
cjyuan marked this conversation as resolved.

Expand All @@ -18,8 +60,4 @@ function playAlarm() {
audio.play();
}

function pauseAlarm() {
audio.pause();
}

Comment on lines -54 to -24
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.

Why delete pauseAlarm()?

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.

Oh no! Have I been careless cleaning up the pause button issue? Not the first time I've confused the script term "pause alarm" vs. the human term "stop alarm", apologies!

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.

We can double check what we have changed in the PR by clicking the "Files changed" tab.

image

window.onload = setup;
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />
<input id="alarmSet" type="number" min="1" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
Expand Down
7 changes: 7 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
.alarm-activated {
background-color: darkorange;
}

#alarmSet {
Expand All @@ -13,3 +15,8 @@
h1 {
text-align: center;
}

body {
background-color: cornflowerblue;
font-family: "Trebuchet MS", Tahoma, Verdana, Helvetica, sans-serif;
}
Loading