Skip to content
Open
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() {}
let countdownId;

// moved to outer scope, takes seconds as a parameter
function updateDisplay(seconds) {
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")}`;
}

// reset before starting new countdown
function resetAlarm() {
clearInterval(countdownId);
audio.pause();
audio.currentTime = 0;
updateDisplay(0);
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() {
resetAlarm();
let seconds = parseInt(document.getElementById("alarmSet").value);

if (!seconds || seconds < 1) {
alert("The number of seconds must be higher than 0 please");
return;
}

updateDisplay(seconds);
// pass seconds as argument and update immediately on click

countdownId = setInterval(() => {
seconds--;
updateDisplay(seconds);
// pass seconds as argument

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

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

Expand Down
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