diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..29116c2c8 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,48 @@ -function setAlarm() {} +let timer = null; +let remaining = 0; -// DO NOT EDIT BELOW HERE +function formatTime(s) { + const m = String(Math.floor(s / 60)).padStart(2, "0"); + const sec = String(s % 60).padStart(2, "0"); + return `${m}:${sec}`; +} + +function setAlarm() { + clearInterval(timer); + audio.pause(); + document.body.style.backgroundColor = ""; -var audio = new Audio("alarmsound.mp3"); + const input = Number(document.getElementById("alarmSet").value) || 0; + remaining = input; -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); - }); + const display = document.getElementById("timeRemaining"); + display.innerText = `Time Remaining: ${formatTime(remaining)}`; - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); -} + if (remaining === 0) { + playAlarm(); + return; + } -function playAlarm() { - audio.play(); + timer = setInterval(() => { + remaining--; + display.innerText = `Time Remaining: ${formatTime(remaining)}`; + + if (remaining <= 0) { + clearInterval(timer); + playAlarm(); + document.body.style.backgroundColor = "orange"; + } + }, 1000); } function pauseAlarm() { - audio.pause(); + clearInterval(timer); } -window.onload = setup; +function stopAlarm() { + clearInterval(timer); + audio.pause(); + audio.currentTime = 0; + document.body.style.backgroundColor = ""; + document.getElementById("timeRemaining").innerText = "Time Remaining: 00:00"; +}