diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..2b07dd67b 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,45 @@ -function setAlarm() {} +function setAlarm(){ + const input = document.getElementById("alarmSet"); + const heading = document.getElementById("timeRemaining"); + + let seconds = parseInt(input.value, 10); + if (!Number.isFinite(seconds) || seconds < 0) seconds = 0; + + function formatMMSS(s) { + const mm = Math.floor(s / 60) + .toString() + .padStart(2, "0"); + const ss = (s % 60).toString().padStart(2, "0"); + return `Time Remaining: ${mm}:${ss}`; + } + + heading.innerText = formatMMSS(seconds); + + if (window._alarmIntervalId) { + clearInterval(window._alarmIntervalId); + window._alarmIntervalId = null; + } + + // If it's already zero, play immediately and don't start an interval + if (seconds === 0) { + playAlarm(); + return; + } + + window._alarmIntervalId = setInterval(() => { + seconds -= 1; + + if (seconds > 0) { + heading.innerText = formatMMSS(seconds); + } else { + // Ensure the heading shows 00:00, stop the interval and play the alarm once + heading.innerText = formatMMSS(0); + clearInterval(window._alarmIntervalId); + window._alarmIntervalId = null; + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..da540358f 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,8 @@ - Title here + Alarm Clock App +
@@ -15,6 +16,5 @@

Time Remaining: 00:00

-