diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..fe2779319 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,40 @@ -function setAlarm() {} +let alarmIntervalId = null; -// DO NOT EDIT BELOW HERE +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; -var audio = new Audio("alarmsound.mp3"); + 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}`; + } -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); - }); + heading.innerText = formatMMSS(seconds); - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); -} + if (alarmIntervalId) { + clearInterval(alarmIntervalId); + alarmIntervalId = null; + } -function playAlarm() { - audio.play(); -} + if (seconds === 0) { + playAlarm(); + return; + } -function pauseAlarm() { - audio.pause(); + alarmIntervalId = setInterval(() => { + seconds -= 1; + if (seconds > 0) { + heading.innerText = formatMMSS(seconds); + } else { + heading.innerText = formatMMSS(0); + clearInterval(alarmIntervalId); + alarmIntervalId = null; + playAlarm(); + } + }, 1000); } window.onload = setup; 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

-