Skip to content
Open
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 timerInterval = null;

function setAlarm() {
const inputEl = document.getElementById("alarmSet");
const timeRemainingEl = document.getElementById("timeRemaining");

let totalSeconds = parseInt(inputEl.value, 10);

// If input is invalid or empty, default to 0
if (isNaN(totalSeconds) || totalSeconds < 0) {
totalSeconds = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am not sure if the expected user behaviour for invalid inputs is to set the seconds to 0. What else could you do instead?

}
// Clear any existing timer if the button is clicked again
if (timerInterval) {
clearInterval(timerInterval);
}
// Function to render the formatted time (MM:SS) to the DOM
function updateDisplay() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job putting the code for formatting and setting the countdown into an own function. This makes it reusable and easier to change the formtting if needed

const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

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

timeRemainingEl.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}
// 1. Immediately set heading on button click
updateDisplay();

// 2. Start the countdown timer
timerInterval = setInterval(() => {
totalSeconds -= 1;

if (totalSeconds >= 0) {
updateDisplay();
}

if (totalSeconds === 0) {
playAlarm();
clearInterval(timerInterval);
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
Loading