diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..f96df8e9a 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,50 @@ -function setAlarm() {} +let countdownInterval +let alarmTimeout; + +function setAlarm() { + const secondsInput = document.getElementById("alarmSet").value; + const totalSeconds = parseInt(secondsInput, 10); + + if (isNaN(totalSeconds) || totalSeconds <= 0) { + alert("Please enter a valid number of seconds."); + return; + } + + // Clear any existing countdown + clearInterval(countdownInterval); + clearTimeout(alarmTimeout); + + const alarmDuration = totalSeconds * 1000; + const endTime = Date.now() + alarmDuration; + + // set alarm + alarmTimeout = setTimeout(() => { + playAlarm(); + }, alarmDuration); + + // Update countdown + const updateCountdown = () => { + const timeLeft = endTime - Date.now(); + + if (timeLeft <= 0) { + clearInterval(countdownInterval); + document.getElementById("timeRemaining").innerText = "Time Remaining: 00:00"; + return; + } + + const minutes = Math.floor((timeLeft / 1000) / 60); + const seconds = Math.floor((timeLeft / 1000) % 60); + + document.getElementById("timeRemaining").innerText = + `Time Remaining: ${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`; + }; + + //start countdown + updateCountdown(); + countdownInterval = setInterval(updateCountdown, 1000); +} + + // DO NOT EDIT BELOW HERE @@ -23,3 +69,4 @@ function pauseAlarm() { } window.onload = setup; + diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..477ca4eeb 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,13 +4,13 @@ - Title here + Alarm Clock

Time Remaining: 00:00

- +