diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..7c954d1c2 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,7 +1,40 @@ -function setAlarm() {} +function setAlarm() { + const input = document.getElementById("alarmSet").value; + + if (!input || input <= 0) { + alert("Please enter a valid number"); + return; + } + + let timeRemaining = parseInt(input); + const display = document.getElementById("timeRemaining"); + + const minutes = Math.floor(timeRemaining / 60); +const seconds = timeRemaining % 60; + +display.textContent = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; + + // Clear any previous timer + clearInterval(countdown); + + countdown = setInterval(() => { + timeRemaining--; + + display.textContent = `Time Remaining: 00:${timeRemaining + .toString() + .padStart(2, "0")}`; + + if (timeRemaining === 0) { + clearInterval(countdown); + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE +let countdown; // 👈 needed for timer control + var audio = new Audio("alarmsound.mp3"); function setup() { @@ -20,6 +53,7 @@ function playAlarm() { function pauseAlarm() { audio.pause(); + clearInterval(countdown); } -window.onload = setup; +window.onload = setup; \ No newline at end of file