diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..44f909551 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,73 @@ -function setAlarm() {} +const oneSecondInMilliseconds = 1000; +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); + if (isNaN(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) { + minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + + const seconds_padded = String(seconds).padStart(2, "0"); + const minutes_paddded = String(minutes).padStart(2, "0"); + + const formatted_seconds = `Time Remaining: ${minutes_paddded}:${seconds_padded}`; + return formatted_seconds; +} + +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 @@ - + - Title here + Alarm clock app