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 @@ - Title here + Alarm clock app
@@ -14,6 +14,7 @@

Time Remaining: 00:00

+
diff --git a/Sprint-3/alarmclock/readme.md b/Sprint-3/alarmclock/readme.md index c00a20c9c..3ff659297 100644 --- a/Sprint-3/alarmclock/readme.md +++ b/Sprint-3/alarmclock/readme.md @@ -6,7 +6,7 @@ First off, once you've branched off `main`, then update the title element in `in You will need to write your implementation in `alarmclock.js`. ## How the clock should work - + When you click the `Set Alarm` button the counter at the top of the screen should change to the number you entered in the `input` field. For example, if the `input` field says `10` then the title should say `Time Remaining: 00:10`. Every one second the title should count down by one. diff --git a/Sprint-3/alarmclock/style.css b/Sprint-3/alarmclock/style.css index 0c72de38b..3482c0e2f 100644 --- a/Sprint-3/alarmclock/style.css +++ b/Sprint-3/alarmclock/style.css @@ -13,3 +13,18 @@ h1 { text-align: center; } + +.finish-countdown { + animation: police 0.5s infinite; +} + +@keyframes police { + from { + background-color: blue; + } + to { + background-color: red; + } +} + +