diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..255987cbd 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,53 @@ -function setAlarm() {} +let countdownInterval = null; +let secondsLeft = 0; + +function setAlarm() { + // Read the minutes from the input + const input = document.getElementById("alarmSet"); + const minutes = parseInt(input.value, 10); + // If nothing useful was entered, do nothing + if (isNaN(minutes) || minutes <= 0) { + return; + } + // Convert to total seconds + secondsLeft = minutes; + // Stop any existing countdown + if (countdownInterval !== null) { + clearInterval(countdownInterval); + countdownInterval = null; + } + // Show starting time right away + updateTimeDisplay(); + + // Start the countdown + countdownInterval = setInterval(() => { + secondsLeft = secondsLeft - 1; + + updateTimeDisplay(); + + if (secondsLeft <= 0) { + // Time's up + clearInterval(countdownInterval); + countdownInterval = null; + secondsLeft = 0; + // Makes sure to show exactly 00:00 + updateTimeDisplay(); + playAlarm(); + } + }, 1000); +} + +// Helper function +function updateTimeDisplay() { + const minutes = Math.floor(secondsLeft / 60); + const seconds = secondsLeft % 60; + + const displayMin = minutes.toString().padStart(2, "0"); + const displaySec = seconds.toString().padStart(2, "0"); + + const heading = document.getElementById("timeRemaining"); + heading.textContent = `Time Remaining: ${displayMin}:${displaySec}`; +} // 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 @@ - +
-