diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..1fdaf20e6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,48 @@ -function setAlarm() {} +let timer; + +function setAlarm() { + const input = document.getElementById("alarmSet").value; + let timeLeft = Number(input); + + updateDisplay(timeLeft); + + if (input === "" || isNaN(timeLeft)) { + return; + } + + if (timeLeft < 0) { + return; + } + + updateDisplay(timeLeft); + + clearInterval(timer); + + timer = setInterval(() => { + timeLeft--; + + updateDisplay(timeLeft); + + if (timeLeft <= 0) { + clearInterval(timer); + playAlarm(); + document.body.style.backgroundColor = "red"; + } + }, 1000); +} + +function updateDisplay(secondsLeft) { + const heading = document.getElementById("timeRemaining"); + + if (secondsLeft < 0) { + secondsLeft = 0; + } + + const minutes = Math.floor(secondsLeft / 60); + const seconds = secondsLeft % 60; + + heading.innerText = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; +} // DO NOT EDIT BELOW HERE