diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..f985e5319 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,104 @@ -function setAlarm() {} +let timer; +let timeLeft = 0; +let paused = false; +const pauseBtn = document.getElementById("pauseAndResume"); +const setBtn = document.getElementById("set"); +const inputField = document.getElementById("alarmSet"); +const audio = new Audio("alarmsound.mp3"); -// DO NOT EDIT BELOW HERE +// update alarm +function updateDisplay() { + const timeDisplay = document.getElementById("timeRemaining"); + const mm = String(Math.floor(timeLeft / 60)).padStart(2, "0"); + const ss = String(timeLeft % 60).padStart(2, "0"); + timeDisplay.textContent = `Time Remaining: ${mm}:${ss}`; +} + +// stop or start flashing +let flashingInterval; +const flashingColors = ["#FF4136", "#2ECC40", "#0074D9", "#FFDC00"]; +function startFlashingBackground() { + let colorIndex = 0; + flashingInterval = setInterval(() => { + document.body.style.backgroundColor = flashingColors[colorIndex]; + colorIndex = (colorIndex + 1) % flashingColors.length; + }, 200); +} +function stopFlashingBackground() { + clearInterval(flashingInterval); + document.body.style.backgroundColor = ""; +} -var audio = new Audio("alarmsound.mp3"); +// start countDown +function startCountdown() { + clearInterval(timer); + timer = setInterval(() => { + if (--timeLeft <= 0) { + clearInterval(timer); + startFlashingBackground(); + playAlarm(); + pauseBtn.style.display = "none"; + updateDisplay(); + } else { + updateDisplay(); + } + }, 1000); +} -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); - }); +// set up alarm +function setAlarm() { + const val = parseInt(inputField.value); + if (!val || val <= 0) { + alert("Please enter a valid number of seconds."); + return; + } - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); + timeLeft = val; + updateDisplay(); + startCountdown(); + + setBtn.style.display = "none"; + pauseBtn.style.display = "inline"; + inputField.style.display = "none"; } function playAlarm() { + audio.currentTime = 0; audio.play(); } +// pause or resume alarm +function pauseResumeHandler() { + if (!paused) { + clearInterval(timer); + pauseBtn.textContent = "Resume"; + } else { + startCountdown(); + pauseBtn.textContent = "Pause"; + } + paused = !paused; +} -function pauseAlarm() { +// stop alarm +function stopAlarm() { + clearInterval(timer); audio.pause(); + audio.currentTime = 0; + stopFlashingBackground(); + timeLeft = 0; + updateDisplay(); + + setBtn.style.display = "inline"; + pauseBtn.style.display = "none"; + inputField.style.display = "inline"; + pauseBtn.textContent = "Pause"; + paused = false; +} + +function setup() { + const stopBtn = document.getElementById("stop"); + setBtn.addEventListener("click", setAlarm); + pauseBtn.addEventListener("click", pauseResumeHandler); + stopBtn.addEventListener("click", stopAlarm); } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..bf5e0b71b 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,16 +4,17 @@ - Title here + Alarm clock app
-

Time Remaining: 00:00

- - - - - +

Time Remaining: 00:00

+
+ + + + +
diff --git a/Sprint-3/alarmclock/style.css b/Sprint-3/alarmclock/style.css index 0c72de38b..693fbdfd7 100644 --- a/Sprint-3/alarmclock/style.css +++ b/Sprint-3/alarmclock/style.css @@ -2,14 +2,20 @@ position: fixed; top: 50%; left: 50%; + margin: auto; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); + text-align: center; + box-shadow: 0px 0px 2px 3px rgba(0, 0, 0, 0.08); + background: #fcfcfc; + padding: 30px; + width: 20%; } - -#alarmSet { - margin: 20px; -} - h1 { text-align: center; + font-size: 1.3rem; +} +input, +button { + font-size: 1.1rem; }