diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..baa823f5e 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,90 @@ -function setAlarm() {} +let intervalId; +let isPaused; +let remainingSeconds = 0; +function setAlarm() { + const alarmSetEl = document.getElementById("alarmSet"); + let totalSeconds = +alarmSetEl.value; + + if ( + totalSeconds <= 0 || + isNaN(totalSeconds) || + !Number.isInteger(totalSeconds) + ) { + alarmSetEl.value = ""; + return; + } + cleanInitialState(); + + function updateCountDown() { + remainingSeconds = totalSeconds; + updateDisplayedTime(remainingSeconds); + + if (totalSeconds <= 0) { + finishCountDown(); + return; + } + + totalSeconds -= 1; + } + + updateCountDown(); + intervalId = setInterval(updateCountDown, 1000); +} + +function pauseCountDown(e) { + if (isPaused === false) { + clearInterval(intervalId); + e.target.textContent = "Unpause"; + isPaused = true; + } else { + e.target.textContent = "Pause"; + isPaused = false; + intervalId = setInterval(() => { + if (remainingSeconds <= 0) { + clearInterval(intervalId); + return; + } + remainingSeconds -= 1; + updateDisplayedTime(remainingSeconds); + + if (remainingSeconds <= 0) { + finishCountDown(); + } + }, 1000); + } +} + +function cleanInitialState() { + isPaused = false; + const pauseButton = document.getElementById("pause-button"); + pauseButton.textContent = "Pause"; + clearInterval(intervalId); + document.body.classList.remove("finish-countdown"); + updateDisplayedTime(0); + document.getElementById("alarmSet").value = ""; + pauseAlarm(); +} + +function updateDisplayedTime(totalSeconds) { + let seconds = totalSeconds % 60; + let minutes = (totalSeconds - seconds) / 60; + + let paddedSeconds = seconds.toString().padStart(2, "0"); + let paddedMinutes = minutes.toString().padStart(2, "0"); + document.getElementById("timeRemaining").innerHTML = + `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; +} + +function finishCountDown() { + document.body.classList.add("finish-countdown"); + playAlarm(); + clearInterval(intervalId); +} + +window.addEventListener("load", function () { + const pauseButton = document.getElementById("pause-button"); + pauseButton.addEventListener("click", pauseCountDown); +}); // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..15f18b5e5 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -