diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..f94f08c82 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,5 +1,61 @@ -function setAlarm() {} +let countdown = null; +function resetAlarm() { + clearInterval(countdown); + countdown = null; + pauseAlarm(); +} + +function showError(message) { + const heading = document.getElementById("errorMessage"); + heading.innerText = message; +} + +function setAlarm() { + const input = document.getElementById("alarmSet"); + let time = Number(input.value); + + resetAlarm(); + + if (isNaN(time) || time < 0 || !Number.isInteger(time)) { + showError("Please enter a whole number of seconds."); + return; + } + + document.getElementById("errorMessage").innerText = ""; + + if (time === 0) { + updateHeading(0); + playAlarm(); + return; + } + + updateHeading(time); + + countdown = setInterval(() => { + time--; + + if (time <= 0) { + updateHeading(0); + clearInterval(countdown); + playAlarm(); + } else { + updateHeading(time); + } + }, 1000); +} + +function updateHeading(time) { + const heading = document.getElementById("timeRemaining"); + + const minutes = Math.floor(time / 60); + const seconds = time % 60; + + const mm = String(minutes).padStart(2, "0"); + const ss = String(seconds).padStart(2, "0"); + + heading.innerText = `Time Remaining: ${mm}:${ss}`; +} // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..d0f768562 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,11 +4,12 @@ -