diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..ac20d702f 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,63 @@ -function setAlarm() {} +let countdownTimer; +let flashInterval; + +function setAlarm() { + document.getElementById("stop").addEventListener("click", () => { + if (flashInterval) { + clearInterval(flashInterval); + flashInterval = null; + document.body.style.backgroundColor = ""; + } + }); + + let alarmTime = Number(document.querySelector("input").value); + + if (!alarmTime || isNaN(alarmTime) || alarmTime <= 0) { + alert("Please enter a valid positive number for the alarm time"); + return; + } + + let timeLeft = alarmTime; + + function formatTime(time) { + let minutes = Math.floor(time / 60); + let seconds = time % 60; + return `${minutes.toString().padStart(2, "0")}:${seconds + .toString() + .padStart(2, "0")}`; + } + + function updateTitle(time) { + document.getElementById( + "timeRemaining" + ).innerHTML = `Time Remaining: ${formatTime(time)}`; + } + + updateTitle(alarmTime); + + function flashingBackground() { + let isRed = false; + flashInterval = setInterval(() => { + document.body.style.backgroundColor = isRed ? "#ffffff" : "#ff4444"; + isRed = !isRed; + }, 400); + } + + if (countdownTimer) { + clearInterval(countdownTimer); + } + + countdownTimer = setInterval(() => { + timeLeft--; + updateTitle(timeLeft); + + if (timeLeft <= 0) { + clearInterval(countdownTimer); + flashingBackground(); + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..cfe17fac0 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm clock app
@@ -15,6 +15,6 @@

Time Remaining: 00:00

- +