Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
52 changes: 51 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
function setAlarm() {}
let timerInterval = null;
let remainingSeconds = 0;

function setAlarm() {
// Read the minutes value from the alarm input field
const minutesInput = document.getElementById("alarmSet");
const minutes = parseInt(minutesInput.value, 10);
// Ignore invalid or non-positive input
if (isNaN(minutes) || minutes <= 0) {
return;
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated
// Store the input as the remaining seconds to count down
remainingSeconds = minutes * 60;
// Clear any existing timer before starting a new one
if (timerInterval !== null) {
clearInterval(timerInterval);
timerInterval = null;
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated
// Immediately render the starting time on screen
updateTimeDisplay();

// Tick every second and decrement remainingSeconds
timerInterval = setInterval(() => {
remainingSeconds = remainingSeconds - 1;

updateTimeDisplay();

if (remainingSeconds <= 0) {
// Countdown finished — stop the timer and trigger the alarm
clearInterval(timerInterval);
timerInterval = null;
remainingSeconds = 0;
// Ensure the display shows exactly 00:00
updateTimeDisplay();
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.

If you modify updateTimeDisplay() to take a parameter, you could call updateTimeDisplay(0) and updateTimeDisplay(remainingTime) without sharing remainingSeconds in the outer/global scope.
You can declare remainingSeconds as a local variable in setAlarm().

Best practice is to avoid sharing variables through outer or global scope.

Designing a function to take input through parameters can also improve the reusability of the function.

Question: Can you elaborate the need to call updateTimeDisplay() on line 34 to "Ensure the display shows exactly 00:00"? Doesn't the function call on line 26 already display "00:00" when remainingTime reaches zero?

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.

Yes, the first updateTimeDisplay() already shows 00:00 when the timer ends.
The second one was only added as a backup, just in case something went slightly wrong with timing.
Having said that, I can see that in a clean implementation it is redundant and not strictly necessary. Thank you.

playAlarm();
}
}, 1000);
}

// Converts remainingSeconds into MM:SS format and updates the display
function updateTimeDisplay() {
const minutes = Math.floor(remainingSeconds / 60);
Comment thread
cjyuan marked this conversation as resolved.
const seconds = remainingSeconds % 60;

const formattedMinutes = minutes.toString().padStart(2, "0");
const formattedSeconds = seconds.toString().padStart(2, "0");

const timeDisplay = document.getElementById("timeRemaining");
timeDisplay.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}


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