diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..094781c1d 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,39 @@ -function setAlarm() {} +let timer; + +function updateDisplay(time) { + + const heading = document.getElementById("timeRemaining"); + const minutes = Math.floor(time / 60); + const seconds = time % 60; + + heading.innerText = + "Time Remaining: " + + String(minutes).padStart(2, "0") + + ":" + + String(seconds).padStart(2, "0"); +} + +function setAlarm() { + let time = Number(document.getElementById("alarmSet").value); + if (!Number.isInteger(time) || time <= 0) return; + + updateDisplay(time); + + if (timer) clearInterval(timer); + audio.pause(); + audio.currentTime = 0; + + timer = setInterval(function () { + time = time - 1; + + updateDisplay(time); + + if (time <= 0) { + playAlarm(); + clearInterval(timer); + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -22,4 +57,4 @@ function pauseAlarm() { audio.pause(); } -window.onload = setup; +window.onload = setup; \ No newline at end of file diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..ff2d3b453 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -