diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..e2d022c14 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,46 @@ -function setAlarm() {} +let timer = null; + +function formatTime(seconds) { + let mins = String(Math.floor(seconds / 60)).padStart(2, "0"); + let secs = String(seconds % 60).padStart(2, "0"); + return `${mins}:${secs}`; +} + +function updateDisplay(timeLeft) { + const display = document.getElementById("timeRemaining"); + display.textContent = `Time Remaining: ${formatTime(timeLeft)}`; +} + +function setAlarm() { + let timeLeft = 0; + const input = document.getElementById("alarmSet").value; + + timeLeft = parseInt(input, 10); + + if (isNaN(timeLeft) || timeLeft <= 0) { + return; + } + + if (timer) { + clearInterval(timer); + timer = null; + } + + stopAlarm(); + + updateDisplay(timeLeft); + + timer = setInterval(() => { + timeLeft--; + updateDisplay(timeLeft); + + if (timeLeft <= 0) { + clearInterval(timer); + timer = null; + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..18a51fc66 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - +
-