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

function setAlarm() {
const input = document.getElementById("alarmSet");
let totalSeconds = parseInt(input.value, 10);

// Return early if input is invalid
if (isNaN(totalSeconds) || totalSeconds <= 0) {
return;
}

// 1. Reset Audio: Stop audio if it's currently playing from a previous alarm
pauseAlarm();
audio.currentTime = 0; // Rewind audio track back to the start

// 2. Reset Interval: Clear existing active countdown
if (countdownInterval) {
clearInterval(countdownInterval);
}
Comment on lines +17 to +19

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.

What other application states should also be reset before starting a new countdown?

Note: 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.

i added the paulse Alarm right after validating the input because If the previous alarm reached zero and audio was currently playing, setting a brand-new timer without clicking the Stop button first would leave the old audio playing in the background while the new countdown started. This stops any active sound immediately. and secondly i (input.value = "") after starting the timer. This cleans up the user interface so the input box doesn't stay pre-filled with old numbers while the countdown is actively running. i don't know if this meet what you mean in the question you asked.

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.

Yes. I was referring to the alarm. Clearing the input field is also a good idea.

Well done!


const heading = document.getElementById("timeRemaining");

function updateDisplay(seconds) {
const minutes = Math.floor(seconds / 60);
const remSeconds = seconds % 60;

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

heading.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}

updateDisplay(totalSeconds);

input.value = "";

countdownInterval = setInterval(() => {
totalSeconds--;
updateDisplay(totalSeconds);

if (totalSeconds <= 0) {
clearInterval(countdownInterval);
playAlarm();
}
}, 1000);
}
// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");
Expand Down
Loading