diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..9e5e8b408 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,64 @@ function setAlarm() {} +const display = document.querySelector("#timeRemaining"); +const totalSeconds = document.querySelector("#alarmSet"); +const setButton = document.querySelector("#set"); +const stopButton = document.querySelector("#stop"); +let countDownInterval; + +function formatTime(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + const paddedMinutes = String(minutes).padStart(2, "0"); + const paddedSeconds = String(seconds).padStart(2, "0"); + return `Time Remaining: ${paddedMinutes}:${paddedSeconds}`; +} + +function reset() { + clearInterval(countDownInterval); + display.textContent = "Time Remaining: 00:00"; + display.style.color = "black"; + setButton.disabled = false; + document.body.style.background = ""; +} + +setButton.addEventListener("click", () => { + let timeLeft = Number(totalSeconds.value); + + if (timeLeft <= 0 || isNaN(timeLeft)) { + alert("please enter a number greater than zero (0)!"); + return; + } + setButton.disabled = true; + + const warningTime = 10; // seconds + + display.textContent = formatTime(timeLeft); + countDownInterval = setInterval(() => { + display.textContent = formatTime(timeLeft); + timeLeft--; + + if (timeLeft >= 0) { + display.textContent = formatTime(timeLeft); + } + + if (timeLeft <= warningTime) { + display.style.color = "red"; + } + + if (timeLeft <= 0) { + clearInterval(countDownInterval); + document.body.style.backgroundColor = "grey"; + playAlarm(); + } + }, 1000); +}); + +stopButton.addEventListener("click", () => { + clearInterval(countDownInterval); + reset(); + pauseAlarm(); +}); // DO NOT EDIT BELOW HERE @@ -8,10 +68,6 @@ function setup() { document.getElementById("set").addEventListener("click", () => { setAlarm(); }); - - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); } function playAlarm() { diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..1d68d420e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - +
-