diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..b6ea392d4 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,59 @@ -function setAlarm() {} +let secondsRemaining = 0; +let timer = null; + +function setAlarm() { + const input = document.getElementById("alarmSet"); + const heading = document.getElementById("timeRemaining"); + + const value = Number(input.value); + + if (!Number.isFinite(value) || value <= 0) { + updateHeading(heading, 0); + return; + } + + secondsRemaining = Math.floor(value); + + if (timer !== null) { + clearInterval(timer); + timer = null; + } + + updateHeading(heading, secondsRemaining); + + timer = setInterval(() => { + secondsRemaining -= 1; + + updateHeading(heading, secondsRemaining); + + if (secondsRemaining === 0) { + clearInterval(timer); + timer = null; + playAlarm(); + } + }, 1000); +} + +function updateHeading(heading, totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + const formattedMinutes = String(minutes).padStart(2, "0"); + const formattedSeconds = String(seconds).padStart(2, "0"); + + heading.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; +} + +function stopTimer() { + if (timer !== null) { + clearInterval(timer); + timer = null; + } +} + +window.addEventListener("load", () => { + document.getElementById("stop").addEventListener("click", stopTimer); +}); // DO NOT EDIT BELOW HERE @@ -22,4 +77,4 @@ function pauseAlarm() { audio.pause(); } -window.onload = setup; +window.onload = setup; \ No newline at end of file diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..5de159cd6 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -