diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..26e529885 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,58 @@ -function setAlarm() {} +let timerId = null; +let remaining = 0; + +function startCountdown() { + timerId = setInterval(() => { + remaining = remaining - 1; + showTime(remaining); + + if (remaining <= 0) { + clearInterval(timerId); + timerId = null; + playAlarm(); + document.body.classList.add("finished"); + } + }, 1000); +} + +function setAlarm() { + clearInterval(timerId); + document.body.classList.remove("finished"); + document.getElementById("pause").innerText = "Pause Alarm"; + remaining = Number(document.getElementById("alarmSet").value); + showTime(remaining); + startCountdown(); +} + +function showTime(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + const mm = String(minutes).padStart(2, "0"); + const ss = String(seconds).padStart(2, "0"); + + document.getElementById("timeRemaining").innerText = + `Time Remaining: ${mm}:${ss}`; +} + +function togglePause() { + const pauseButton = document.getElementById("pause"); + + if (timerId !== null) { + clearInterval(timerId); + timerId = null; + pauseButton.innerText = "Resume Alarm"; + } else if (remaining > 0) { + startCountdown(); + pauseButton.innerText = "Pause Alarm"; + } +} + +document.getElementById("pause").addEventListener("click", togglePause); + +document.getElementById("stop").addEventListener("click", () => { + document.body.classList.remove("finished"); +}); // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..4576eda16 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - +
-