diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..9372aa9eb 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,92 @@ -function setAlarm() {} +const titleElement = document.getElementById("timeRemaining"); + +const ONE_SECOND_IN_MILLISECONDS = 1000; + +let timeRemainingInSeconds = 0; +let alarmTimerIdentifier = null; + +/** + * Formats seconds into "MM:SS". + */ +function formatTime(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + const formattedMinutes = String(minutes).padStart(2, '0'); + const formattedSeconds = String(seconds).padStart(2, '0'); + + return `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; +} + +/** + * Displays the remaining time. + */ +function displayTime() { + titleElement.innerText = formatTime(timeRemainingInSeconds); +} + +/** + * Updates the display and checks if the alarm should sound. + */ +function updateTime() { + timeRemainingInSeconds = timeRemainingInSeconds - 1; + displayTime(); + + if (timeRemainingInSeconds === 0) { + playAlarm(); + document.body.classList.add("flash"); + clearInterval(alarmTimerIdentifier); + alarmTimerIdentifier = null; + } +} + +/** + * Initialises the alarm countdown. + */ +function setAlarm() { + if (alarmTimerIdentifier) { + clearInterval(alarmTimerIdentifier); + document.body.classList.remove("flash"); + } + + const timeInput = document.getElementById("alarmSet"); + const startingTime = parseInt(timeInput.value, 10); + + if (isNaN(startingTime)) { + return; + } + + if (startingTime < 0) { + return; + } + + timeRemainingInSeconds = startingTime; + + // Initialise the audio context to enable autoplay. + audio.play(); + audio.pause(); + + displayTime(); + + // Handle zero-second edge case + if (timeRemainingInSeconds === 0) { + playAlarm(); + document.body.classList.add("flash"); + return; + } + + alarmTimerIdentifier = setInterval(() => { + updateTime(); + }, ONE_SECOND_IN_MILLISECONDS); +} + +// Add a listener to stop the flashing effect without modifying the protected pauseAlarm function below. +const stopButton = document.getElementById("stop"); +if (stopButton) { + stopButton.addEventListener("click", () => { + document.body.classList.remove("flash"); + }); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..c0ae6b482 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,19 +1,24 @@ - +
-