diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..b65c9af01 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,9 +1,59 @@ -function setAlarm() {} +let countdown = null; +let remainingSeconds = 0; + +var audio = new Audio("alarmsound.mp3"); +audio.loop = true; +audio.currentTime = 0; + + +function setAlarm() { + const input = document.getElementById("alarmSet"); + const timeText = document.getElementById("timeRemaining"); + + const seconds = parseInt(input.value, 10); + + if (isNaN(seconds) || seconds <= 0) { + alert("Enter a number greater than 0"); + return; + } + + // Stop any previous countdown + if (countdown !== null) { + clearInterval(countdown); + } + + remainingSeconds = seconds; + updateTimeDisplay(); + + countdown = setInterval(() => { + remainingSeconds--; + + updateTimeDisplay(); + + if (remainingSeconds <= 0) { + clearInterval(countdown); + countdown = null; + playAlarm(); + document.body.style.backgroundColor = "#f99"; // change background + } + }, 1000); +} + +function updateTimeDisplay() { + const minutes = String(Math.floor(remainingSeconds / 60)).padStart(2, "0"); + const seconds = String(remainingSeconds % 60).padStart(2, "0"); + document.getElementById("timeRemaining").textContent = `Time Remaining: ${minutes}:${seconds}`; +} + document.getElementById("stop").addEventListener("click", () => { + document.body.style.backgroundColor = ""; // reset background + }); // DO NOT EDIT BELOW HERE -var audio = new Audio("alarmsound.mp3"); +function pauseAlarm() { + audio.pause(); +} function setup() { document.getElementById("set").addEventListener("click", () => { setAlarm(); @@ -18,8 +68,5 @@ function playAlarm() { audio.play(); } -function pauseAlarm() { - audio.pause(); -} window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..437e66a17 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,17 +4,20 @@ -