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
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 timer = null;

function formatTime(seconds) {
let mins = String(Math.floor(seconds / 60)).padStart(2, "0");
let secs = String(seconds % 60).padStart(2, "0");
return `${mins}:${secs}`;
}

function updateDisplay(timeLeft) {
const display = document.getElementById("timeRemaining");
display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
}

function setAlarm() {
let timeLeft = 0;
const input = document.getElementById("alarmSet").value;

timeLeft = parseInt(input, 10);

if (isNaN(timeLeft) || timeLeft <= 0) {
return;
}

if (timer) {
clearInterval(timer);
timer = null;
}
Comment on lines +24 to +27
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 14, 2026

Choose a reason for hiding this comment

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

What elsse should also be reset before a new countdown begin?

Hint: a user may not click the "Stop" button first before starting a new count down.

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.

Good point I’ve reset the timer reference to null after clearing it to ensure a clean state before starting a new countdown.

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.

  • Setting timer to null is unnecessary in JS but it is a good practice.

  • What does the "Stop" button do? What would happen when a countdown reaches zero, and the user immediately set another 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.

The Stop button clears the timer and stops the alarm. If a countdown reaches zero and the user immediately starts another one, the alarm may still be playing. So we should also reset/stop the alarm when starting a new countdown to keep the behavior consistent.


stopAlarm();

updateDisplay(timeLeft);

timer = setInterval(() => {
timeLeft--;
updateDisplay(timeLeft);

if (timeLeft <= 0) {
clearInterval(timer);
timer = null;
playAlarm();
}
}, 1000);
}
Comment thread
cjyuan marked this conversation as resolved.

// DO NOT EDIT BELOW HERE

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
Loading