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
76 changes: 75 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
function setAlarm() {}
let timerId = null;
let remainingSeconds = 0;
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.

Best practice is to avoid declaring variable in the global/outermost scope.
You can declare remainingSeconds as a local variable in setAlarm() and then pass it via a parameter to updateHeading().


function pad(n) {
return String(n).padStart(2, "0");
}

function updateHeading() {
const heading = document.getElementById("timeRemaining");
if (!heading) return;
const m = Math.floor(remainingSeconds / 60);
const s = remainingSeconds % 60;
heading.textContent = `Time Remaining: ${pad(m)}:${pad(s)}`;
}

function clearExistingTimer() {
if (timerId !== null) {
clearInterval(timerId);
timerId = null;
}
}

function setAlarm() {
const input = document.getElementById("alarmSet");
const raw = input ? input.value : "0";
remainingSeconds = Math.max(0, Math.floor(Number(raw)) || 0);

updateHeading();

clearExistingTimer();

if (remainingSeconds === 0) {
playAlarm();
triggerVisualAlarm();
return;
}

timerId = setInterval(() => {
remainingSeconds--;
updateHeading();

if (remainingSeconds <= 0) {
clearExistingTimer();
playAlarm();
triggerVisualAlarm();
}
}, 1000);
}

// Visual feedback helpers (add/remove a class on <body>)
function triggerVisualAlarm() {
try {
document.body.classList.add("alarm-active");
} catch (e) {
// ignore if DOM not available in test environment
}
}

function clearVisualAlarm() {
try {
document.body.classList.remove("alarm-active");
} catch (e) {
// ignore if DOM not available in test environment
}
}
Comment on lines +50 to +65
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 5, 2026

Choose a reason for hiding this comment

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

Nice touch in using a CSS class to manage animated background.

Could also consider combining this into one function and use a parameter to control whether to set or clear visual alarm, and set the default parameter value to "clear" or false.


// Ensure visual alarm is cleared when user stops the alarm or sets a new one.
try {
window.addEventListener("load", () => {
const stopBtn = document.getElementById("stop");
if (stopBtn) stopBtn.addEventListener("click", clearVisualAlarm);

const setBtn = document.getElementById("set");
if (setBtn) setBtn.addEventListener("click", clearVisualAlarm);
});
} catch (e) {}
// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");
Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<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
18 changes: 18 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@
h1 {
text-align: center;
}

/* Visual alarm state: make the background flash and tint red */
.alarm-active {
animation: alarm-flash 1s linear infinite;
background-color: rgba(255, 0, 0, 0.12);
}

@keyframes alarm-flash {
0% {
box-shadow: inset 0 0 0 rgba(255, 0, 0, 0);
}
50% {
box-shadow: inset 0 0 2000px rgba(255, 0, 0, 0.08);
}
100% {
box-shadow: inset 0 0 0 rgba(255, 0, 0, 0);
}
}
Loading