diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/Alarm-clock-app.html similarity index 100% rename from Sprint-3/alarmclock/index.html rename to Sprint-3/alarmclock/Alarm-clock-app.html diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..092769101 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,5 +1,49 @@ -function setAlarm() {} +let countdownInterval = null; +function setAlarm() { + const input = document.getElementById("alarmSet"); + let totalSeconds = parseInt(input.value, 10); + + // Return early if input is invalid + if (isNaN(totalSeconds) || totalSeconds <= 0) { + return; + } + + // 1. Reset Audio: Stop audio if it's currently playing from a previous alarm + pauseAlarm(); + audio.currentTime = 0; // Rewind audio track back to the start + + // 2. Reset Interval: Clear existing active countdown + if (countdownInterval) { + clearInterval(countdownInterval); + } + + const heading = document.getElementById("timeRemaining"); + + function updateDisplay(seconds) { + const minutes = Math.floor(seconds / 60); + const remSeconds = seconds % 60; + + const formattedMinutes = String(minutes).padStart(2, "0"); + const formattedSeconds = String(remSeconds).padStart(2, "0"); + + heading.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; + } + + updateDisplay(totalSeconds); + + input.value = ""; + + countdownInterval = setInterval(() => { + totalSeconds--; + updateDisplay(totalSeconds); + + if (totalSeconds <= 0) { + clearInterval(countdownInterval); + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3");