diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..b9f1f1f58 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,49 @@ -function setAlarm() {} +let timer; + +function setAlarm() { + const timeSet = document.getElementById("alarmSet").value; + countdown(timeSet); +} + +function countdown(time) { + clearInterval(timer); + let timeRemaining = time; + if (timeRemaining > 5999) { + timeRemaining = 5999; + } + updateShownTime(timeRemaining); + + timer = setInterval(() => { + timeRemaining -= 1; + if (timeRemaining == 0) { + clearInterval(timer); + playAlarm(); + } + updateShownTime(timeRemaining); + }, 1000); +} + +function updateShownTime(timeRemaining) { + let timeRemainingOutput = document.getElementById("timeRemaining"); + let title = document.getElementById("title"); + const formattedTime = convertTime(timeRemaining); + const printedTime = printTime(formattedTime); + timeRemainingOutput.textContent = `Time Remaining: ${printedTime}`; + title.textContent = `Time Remaining: ${printedTime}`; +} + +function convertTime(timeSeconds) { + const seconds = timeSeconds % 60; + const minutes = (timeSeconds - seconds) / 60; + console.log([minutes, seconds]); + return [minutes, seconds]; +} + +function printTime(time) { + const paddedHours = String(time[0]).padStart(2, "0"); + const paddedSeconds = String(time[1]).padStart(2, "0"); + return paddedHours + ":" + paddedSeconds; +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..075ae1c58 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -