diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..350f699e5 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,8 +1,38 @@ function setAlarm() {} +let timer; +function formatTime(totalSeconds) { + let minutes = Math.floor(totalSeconds / 60); + let seconds = totalSeconds % 60; + return ( + String(minutes).padStart(2, "0") + ":" + String(seconds).padStart(2, "0") + ); +} +function setAlarm() { + clearInterval(timer); + let seconds = Number(document.getElementById("alarmSet").value); + const title = document.getElementById("timeRemaining"); + + if (!(seconds > 0)) { + title.innerText = "Time Remaining: 00:00"; + return; + } + + title.innerText = "Time Remaining: " + formatTime(seconds); + + timer = setInterval(() => { + seconds = seconds - 1; + title.innerText = "Time Remaining: " + formatTime(seconds); + + if (seconds <= 0) { + clearInterval(timer); + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE -var audio = new Audio("alarmsound.mp3"); +let audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { 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 @@ - +
-