diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/alarmclock.html similarity index 85% rename from Sprint-3/alarmclock/index.html rename to Sprint-3/alarmclock/alarmclock.html index 48e2e80d9..2dbf5407e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/alarmclock.html @@ -4,7 +4,7 @@ - Title here + Alarm clock app
@@ -13,6 +13,7 @@

Time Remaining: 00:00

+
diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..9035e3b32 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,10 +1,38 @@ -function setAlarm() {} +function setAlarm() { + const timer = Number(document.querySelector("#alarmSet").value); + const timeRemainingEl = document.querySelector("#timeRemaining"); + let targetTime = new Date().getTime() + timer * 1000; + + const doAlarm = setInterval( () => { + const timeThisSecond = new Date().getTime(); + const timeDifference = targetTime - timeThisSecond; + const secondsLeft = Math.max(0, Math.ceil(timeDifference / 1000)); + + timeRemainingEl.innerHTML = `Time Remaining: ${formatTimeMMSS(secondsLeft)}`; + + if (secondsLeft === 0) { + playAlarm(); + clearInterval(doAlarm); + } + }, 1000); +} + +function formatTimeMMSS (seconds) { + const minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + return `${String(minutes).padStart(2,"0")}:${String(seconds).padStart(2,"0")}`; +} // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); function setup() { + document.getElementById("pause").addEventListener("click", () => { + pauseAlarm(); + clearInterval(doAlarm); + }); + document.getElementById("set").addEventListener("click", () => { setAlarm(); });