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: 44 additions & 0 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
function setAlarm() {}
// Only define Audio if it doesn't exist (Node environment)
if (typeof Audio === "undefined") {
global.Audio = class {
constructor(src) {
this.src = src;
}
play() {}
pause() {}
};
}

let intervalId;

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

function setAlarm() {
const input = document.getElementById("alarmSet");
const timer = document.getElementById("timeRemaining");

const seconds = parseInt(input.value);
if (isNaN(seconds) || seconds < 0) return;

let remaining = seconds;

// DO NOT EDIT BELOW HERE
timer.textContent = `Time Remaining: ${formatTime(remaining)}`;

if (intervalId) clearInterval(intervalId);

intervalId = setInterval(() => {
remaining--;

timer.textContent = `Time Remaining: ${formatTime(remaining)}`;

if (remaining <= 0) {
clearInterval(intervalId);
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
Loading