Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 49 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
function setAlarm() {}
// Stores the interval ID so we can clear it later
let interval = null;

function setAlarm() {
// Clear any previously running countdown
clearInterval(interval);

// Reset background to default
document.body.style.backgroundColor = "";

Comment thread
cjyuan marked this conversation as resolved.
// Read the input value and convert it to a plain integer (total seconds)
let totalSeconds = parseInt(document.getElementById("alarmSet").value);
Comment thread
cjyuan marked this conversation as resolved.

// --- Format and display the initial time ---
// Math.floor(119 / 60) = 1 --> minutes
// 119 % 60 = 59 --> leftover seconds
// padStart(2, "0") ensures single digits become "01", "09", etc.
const mins = String(Math.floor(totalSeconds / 60)).padStart(2, "0");
const secs = String(totalSeconds % 60).padStart(2, "0");
document.getElementById("timeRemaining").innerText =
"Time Remaining: " + mins + ":" + secs;
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.

Instead of keeping several copies of the code for displaying time (lines 18-21, lines 44-47, lines 32-33),
it is better to define a function to update the time display and call the function to update the time display.

Advantages:

  • Keep the logic for updating time display in one place
  • Less code


// --- Start the countdown ---
// setInterval calls the callback every 1000 ms (1 second) and returns an ID
interval = setInterval(function () {
totalSeconds--; // subtract 1 second

if (totalSeconds <= 0) {
// Stop the interval when time runs out
clearInterval(interval);

document.getElementById("timeRemaining").innerText =
"Time Remaining: 00:00";

// Change background color to signal the alarm
document.body.style.backgroundColor = "red";
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.

To better separate presentation logic from application logic, you can consider defining a CSS class, and use classList.toggle() to apply/remove the style. For example,

document.body.classList.toggle("alarm-activated", true);  // apply style
document.body.classList.toggle("alarm-activated", false); // remove style


// Trigger the alarm sound (defined below the DO NOT EDIT line)
playAlarm();
return;
}

// Recalculate mm:ss for the new totalSeconds and update the display
const m = String(Math.floor(totalSeconds / 60)).padStart(2, "0");
const s = String(totalSeconds % 60).padStart(2, "0");
document.getElementById("timeRemaining").innerText =
"Time Remaining: " + m + ":" + s;
}, 1000);
}

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