diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..4daa8f240 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,41 @@ -function setAlarm() {} +let currentIntervalId = null; + +function setAlarm() { + const input = document.getElementById("alarmSet"); + const heading = document.getElementById("timeRemaining"); + + let secondsRemaining = Number(input.value); + + if (!secondsRemaining || secondsRemaining <= 0) { + heading.innerText = "Please enter a number of seconds greater than 0"; + return; + } + + function updateDisplay() { + const minutes = Math.floor(secondsRemaining / 60); + const seconds = secondsRemaining % 60; + const paddedMinutes = String(minutes).padStart(2, "0"); + const paddedSeconds = String(seconds).padStart(2, "0"); + heading.innerText = Time Remaining: ${paddedMinutes}:${paddedSeconds}; + } + + updateDisplay(); + + if (currentIntervalId !== null) { + clearInterval(currentIntervalId); + } + + currentIntervalId = setInterval(() => { + secondsRemaining--; + updateDisplay(); + + if (secondsRemaining <= 0) { + clearInterval(currentIntervalId); + currentIntervalId = null; + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -23,3 +60,4 @@ function pauseAlarm() { } window.onload = setup; + diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..4a91379d3 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm Clock App