diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..84e6c3807 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,70 @@ -function setAlarm() {} +let countdownId = null; +const originalBackgroundColor = document.body.style.backgroundColor || "white"; // Store the original background color +let timeRemainingHeading = null; + +function getTimeRemainingHeading() { + if (timeRemainingHeading === null) { + timeRemainingHeading = document.getElementById("timeRemaining"); + } + + return timeRemainingHeading; +} + +function formatTime(totalSeconds) { + // Convert total seconds to minutes and seconds + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; +} + +function updateHeading(totalSeconds) { + // Update the heading with the formatted time remaining + const heading = getTimeRemainingHeading(); // Query once, then reuse the cached DOM node on each tick + heading.innerText = `Time Remaining: ${formatTime(totalSeconds)}`; // Set the text of the heading to show the time remaining in the format "Time Remaining: MM:SS" +} + +function setAlarm() { + const input = document.getElementById("alarmSet"); // Get the input element using its DOM ID + let remainingSeconds = Number(input.value); // Convert the input value to a number representing the total seconds for the countdown + + // Guard against edge cases: empty input, non-numeric, negative values, and 0 + if (!Number.isFinite(remainingSeconds) || remainingSeconds <= 0) { + // Check if the input is a valid number and strictly greater than 0 + alert("Please enter a positive number greater than 0"); + return; // Exit early without starting the countdown + } + + if (countdownId !== null) { + // If there is an existing countdown, clear it before starting a new one + + clearInterval(countdownId); + } + + + // Reset background and stop alarm sound when setting a new alarm + document.body.style.backgroundColor = originalBackgroundColor; + + updateHeading(remainingSeconds); // Update the heading to show the initial time remaining before starting the countdown + + countdownId = setInterval(() => { + // Start a new interval that will execute the provided function every 1000 milliseconds (1 second) + remainingSeconds -= 1; + + if (remainingSeconds <= 0) { + // If the remaining seconds reach 0 or below, stop the countdown and play the alarm + updateHeading(0); + clearInterval(countdownId); + countdownId = null; + playAlarm(); + document.body.style.backgroundColor = "red"; // change background colour + + return; // Exit the function to prevent further execution + } + + updateHeading(remainingSeconds); + }, 1000); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..73760b8fc 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,14 +1,14 @@ - + - Title here + Alarm Clock App
-

Time Remaining: 00:00

+

Time Remaining: 00:00