diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..dd4deae69 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,8 +1,85 @@ -function setAlarm() {} +let countdownInterval; +let flashInterval; + +let audio = new Audio("assets/trebolClan.mp3"); +let stopAudio = new Audio("assets/stopAlarm.mp3"); + +// helper to reset the app to its initial state +function resetApp() { + clearInterval(countdownInterval); + clearInterval(flashInterval); + document.body.style.backgroundColor = "white"; + + + if(audio){ + audio.pause(); + audio.currentTime = 0; + } +} + +//function to changing colors animation. +function startBackgroundAnimation() { + let repetitions = 0; + flashInterval = setInterval(() => { + document.body.style.backgroundColor = `rgb( + ${Math.floor(Math.random() * 256)}, + ${Math.floor(Math.random() * 256)}, + ${Math.floor(Math.random() * 256)})`; + + repetitions++; + if (repetitions > 100) { + clearInterval(flashInterval); + document.body.style.backgroundColor = "white"; + } + }, 200); +} + + +function setAlarm() { + resetApp(); + + const rawValue = document.getElementById("alarmSet").value; + const inputTime = Number(rawValue); + + if (!Number.isInteger(inputTime) || inputTime <= 0) { + alert("Please type or select a valid positive time in seconds 👇⏰"); + return; + } + + let timeLeft = inputTime; + updateDisplay(timeLeft); + + countdownInterval = setInterval(() => { + timeLeft--; + + if (timeLeft <= 0) { + clearInterval(countdownInterval); + updateDisplay(0); + playAlarm(); + + startBackgroundAnimation(); + + } else { + updateDisplay(timeLeft); + } + }, 1000); +} + +function updateDisplay(seconds) { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + + const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes; + const formattedSeconds = + remainingSeconds < 10 ? `0${remainingSeconds}` : remainingSeconds; + + const display = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; + + document.getElementById("timeRemaining").innerText = display; +} // DO NOT EDIT BELOW HERE -var audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { @@ -19,7 +96,8 @@ function playAlarm() { } function pauseAlarm() { - audio.pause(); + resetApp(); + stopAudio.play(); } window.onload = setup; diff --git a/Sprint-3/alarmclock/assets/favicon.png b/Sprint-3/alarmclock/assets/favicon.png new file mode 100644 index 000000000..b3730e026 Binary files /dev/null and b/Sprint-3/alarmclock/assets/favicon.png differ diff --git a/Sprint-3/alarmclock/assets/stopAlarm.mp3 b/Sprint-3/alarmclock/assets/stopAlarm.mp3 new file mode 100644 index 000000000..f476db37d Binary files /dev/null and b/Sprint-3/alarmclock/assets/stopAlarm.mp3 differ diff --git a/Sprint-3/alarmclock/assets/trebolClan.mp3 b/Sprint-3/alarmclock/assets/trebolClan.mp3 new file mode 100644 index 000000000..c15e9d3df Binary files /dev/null and b/Sprint-3/alarmclock/assets/trebolClan.mp3 differ diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..af7703edf 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,13 +4,19 @@ -