diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..8f4632462 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,74 @@ -function setAlarm() {} +let timeRemaining = 0; +let intervalId = null; +let isPaused = false; + + +function setAlarm() { + const input = document.getElementById("alarmSet"); + + const seconds = Number(input.value); + + if (isNaN(seconds) || seconds <= 0) { + alert("Please enter a valid number of seconds."); + return; + } + + timeRemaining = seconds; + updateDisplay(timeRemaining); + + // Clear any previous countdown + if (intervalId) { + clearInterval(intervalId); + } + + // Start a new countdown + intervalId = setInterval(() => { + timeRemaining--; + updateDisplay(timeRemaining); + + if (timeRemaining <= 0) { + clearInterval(intervalId); + playAlarm(); + document.body.classList.add("flash"); + } + }, 1000); +} + +function updateDisplay(seconds) { + const title = document.getElementById("timeRemaining"); + + + const mins = String(Math.floor(seconds / 60)).padStart(2, "0"); + const secs = String(seconds % 60).padStart(2, "0"); + + title.textContent = `Time Remaining: ${mins}:${secs}`; +} +function togglePause() { + if (!isPaused) { + clearInterval(intervalId); + isPaused = true; + } else { + isPaused = false; + intervalId = setInterval(() => { + timeRemaining--; + updateDisplay(timeRemaining); + + if (timeRemaining <= 0) { + clearInterval(intervalId); + playAlarm(); + document.body.classList.add("flash"); + } + }, 1000); + } +} + +document.getElementById("pause").addEventListener("click", togglePause); + + +document.getElementById("stop").addEventListener("click", () => { + document.body.classList.remove("flash"); +}); + // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..c4fe7779d 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -