diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..5ee45b37c 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,73 @@ -function setAlarm() {} +const lengthOfAlarmSound = 20000; + +let timeLeft; +let intervalID; +let timeoutID; +let display; + +function setAlarm() { + stopAlarm(); + + // set up time + timeLeft = document.getElementById("alarmSet").value; + display = document.getElementById("timeRemaining"); + + // validate result + const result = parseInt(timeLeft); + console.log(result); + if (isNaN(result) || result < 0 || !Number.isInteger(result)) { + return; + } + + display.textContent = formatTime(timeLeft); + + if (!intervalID) { + intervalID = setInterval(updateTimer, 1000); + } + + console.log(`Alarm set for ${timeLeft} seconds`); +} + +function updateTimer() { + timeLeft = timeLeft - 1; + + display.textContent = formatTime(timeLeft); + + if (timeLeft <= 0) { + alarmFinished(); + cleanUp(); + } +} + +// give a time in seconds +function formatTime(seconds) { + const minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + + const secondsPadded = String(seconds).padStart(2, "0"); + const minutesPaddded = String(minutes).padStart(2, "0"); + + const formattedPeconds = `Time Remaining: ${minutesPaddded}:${secondsPadded}`; + return formattedPeconds; +} + +function stopAlarm() { + pauseAlarm(); + cleanUp(); +} + +function cleanUp() { + clearInterval(intervalID); + clearTimeout(timeoutID); + timeoutID = null; + intervalID = null; +} + +function alarmFinished() { + audio.loop = true; + playAlarm(); + console.log("Alarm finished"); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..66748001e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - +
-