diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..06dea09dd 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,84 @@ -function setAlarm() {} +// Stores the interval ID so we can clear it later +let interval = null; + +// Helper function: format seconds as mm:ss and update the display +// This keeps the time-display logic in ONE place (CJ feedback: avoid repetition) +function updateTimeDisplay(totalSeconds) { + const mins = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); + const secs = String(totalSeconds % 60).padStart(2, "0"); + document.getElementById("timeRemaining").innerText = + "Time Remaining: " + mins + ":" + secs; +} + +// Helper function: show or clear the feedback message in a separate element +function showFeedback(message) { + document.getElementById("feedback").innerText = message; +} + +function setAlarm() { + // Clear any previously running countdown + clearInterval(interval); + + // Remove alarm style and clear feedback message + document.body.classList.toggle("alarm-activated", false); + showFeedback(""); + + // Stop any currently playing alarm sound before starting a new countdown + // (the user may not click "Stop" first before setting a new alarm) + pauseAlarm(); + + // Read the input value and convert it to a plain integer (total seconds) + let totalSeconds = parseInt(document.getElementById("alarmSet").value); + + // Validate input: reject NaN, negative numbers, or empty field + if (isNaN(totalSeconds) || totalSeconds < 0) { + showFeedback("Please enter a valid number of seconds (0 or above)."); + return; + } + + // If input is 0, trigger the alarm immediately instead of waiting 1 second + if (totalSeconds === 0) { + updateTimeDisplay(0); + document.body.classList.toggle("alarm-activated", true); + playAlarm(); + return; + } + + // Display the initial time using the helper function + updateTimeDisplay(totalSeconds); + + // Start the countdown: setInterval calls the callback every 1000 ms (1 second) + interval = setInterval(function () { + totalSeconds--; // subtract 1 second + + if (totalSeconds <= 0) { + // Stop the interval when time runs out + clearInterval(interval); + + updateTimeDisplay(0); + + // Apply alarm style using a CSS class (separates presentation from logic) + document.body.classList.toggle("alarm-activated", true); + + // Trigger the alarm sound (defined below the DO NOT EDIT line) + playAlarm(); + return; + } + + // Update the display with the new remaining time + updateTimeDisplay(totalSeconds); + }, 1000); +} + +// Make the Stop button also stop the countdown (not just the sound) +// Uses DOMContentLoaded so the button element exists before we attach the listener +// This adds a SECOND listener without modifying the original code below +document.addEventListener("DOMContentLoaded", function () { + document.getElementById("stop").addEventListener("click", function () { + clearInterval(interval); + document.body.classList.toggle("alarm-activated", false); + }); +}); // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..d2b886e5f 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,14 +1,15 @@ - + - Title here + Alarm clock app

Time Remaining: 00:00

+ diff --git a/Sprint-3/alarmclock/style.css b/Sprint-3/alarmclock/style.css index 0c72de38b..351cd8e7c 100644 --- a/Sprint-3/alarmclock/style.css +++ b/Sprint-3/alarmclock/style.css @@ -13,3 +13,13 @@ h1 { text-align: center; } + +#feedback { + text-align: center; + color: red; + font-weight: bold; +} + +.alarm-activated { + background-color: red; +}