diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..520eef21f 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,17 +1,94 @@ -function setAlarm() {} +// DOM elements +const setAlarmBtn = document.getElementById("set"); +const stopAlarmBtn = document.getElementById("stop"); +const timeRemaining = document.getElementById("timeRemaining"); +let alarmInterval; + +// Update Display function; +function updateDisplay(secondsValue) { + let minutes = Math.floor(secondsValue / 60); + let seconds = secondsValue % 60; + timeRemaining.innerText = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; +} + +// reset function - returns app to clean initial state +function resetAlarm() { + function resetToInitialState() { + updateDisplay(0); + document.getElementById("alarmSet").value = ""; + stopAlarmBtn.style.display = "none"; + setAlarmBtn.style.display = "inline-block"; // ADD THIS - show set button + } + + function stopSound() { + if (typeof pauseAlarm === "function") { + pauseAlarm(); + } + } + // Clear any running interval + if (alarmInterval) { + clearInterval(alarmInterval); + alarmInterval = null; + } + + resetToInitialState(); + stopSound(); +} + +// Set Alarm function +function setAlarm() { + let totalSeconds; + const input = document.getElementById("alarmSet"); + const timeValue = parseInt(input.value); + const hasActiveTimer = alarmInterval !== null; + + function startTimer() { + alarmInterval = setInterval(() => { + totalSeconds--; + updateDisplay(totalSeconds); + if (totalSeconds === 0) { + clearInterval(alarmInterval); + alarmInterval = null; + + // WHEN ALARM STARTS: hide set button, show only stop button + setAlarmBtn.style.display = "none"; + stopAlarmBtn.style.display = "inline-block"; + + playAlarm(); + } + }, 1000); + } + + resetAlarm(); + + if (isNaN(timeValue) || timeValue <= 0) { + if (!hasActiveTimer) alert("Please enter a number greater than 0"); + return; + } + + totalSeconds = timeValue; + stopAlarmBtn.style.display = "inline-block"; + setAlarmBtn.style.display = "inline-block"; // Ensure set button is visible + updateDisplay(totalSeconds); + startTimer(); +} + +document.getElementById("stop").addEventListener("click", () => { + pauseAlarm(); + resetAlarm(); +}); // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); function setup() { + // Ensure clean initial state when page loads + resetAlarm(); + 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..696febed8 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - +
-