diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b1..60ea31d498 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,8 +1,58 @@ -function setAlarm() {} +let intervalId = null; + +function formatTime(totalSeconds) { + const mins = Math.floor(totalSeconds / 60); + const secs = totalSeconds % 60; + return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; +} + +function finishAlarm() { + if (intervalId !== null) { + clearInterval(intervalId); + intervalId = null; + } + const heading = document.getElementById("timeRemaining"); + heading.textContent = "Time Remaining: 00:00"; + playAlarm(); + document.body.style.backgroundColor = "red"; +} + +function setAlarm() { + if (intervalId !== null) { + clearInterval(intervalId); + intervalId = null; + } + + const input = document.getElementById("alarmSet"); + let remaining = Number(input.value); + + if (!Number.isFinite(remaining) || remaining < 0) { + return; + } + + const heading = document.getElementById("timeRemaining"); + heading.textContent = `Time Remaining: ${formatTime(remaining)}`; + + // 0 seconds: trigger immediately at 00:00 + if (remaining === 0) { + finishAlarm(); + return; + } + + intervalId = setInterval(() => { + remaining -= 1; + heading.textContent = `Time Remaining: ${formatTime(remaining)}`; + + if (remaining <= 0) { + finishAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); +audio.loop = true; function setup() { document.getElementById("set").addEventListener("click", () => { @@ -11,6 +61,7 @@ function setup() { document.getElementById("stop").addEventListener("click", () => { pauseAlarm(); + document.body.style.backgroundColor = "white"; }); } @@ -20,6 +71,7 @@ function playAlarm() { function pauseAlarm() { audio.pause(); + audio.currentTime = 0; } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d99..ff2d3b4532 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -