diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..152579f5a 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,54 @@ -function setAlarm() {} +function setAlarm() { + // Get the input value + let time = parseInt(document.getElementById("alarmSet").value) || 0; + + // Check for negative numbers + if (time < 0) { + document.getElementById("timeRemaining").textContent = + "Time Remaining: 00:00"; + return; // Exit the function if the input is negative + } + + // Clear any existing interval + if (window.alarmInterval) { + clearInterval(window.alarmInterval); + } + + // Update the display immediately + document.getElementById("timeRemaining").textContent = + "Time Remaining: " + formatTime(time); + + // Start countdown + window.alarmInterval = setInterval(function () { + // Decrement time first + time--; + + // Check if time is zero or negative + if (time <= 0) { + clearInterval(window.alarmInterval); + document.getElementById("timeRemaining").textContent = + "Time Remaining: 00:00"; + playAlarm(); + return; + } + + // Update display after checking + document.getElementById("timeRemaining").textContent = + "Time Remaining: " + formatTime(time); + }, 1000); +} + +// Helper function to format time as MM:SS +function formatTime(seconds) { + var minutes = Math.floor(seconds / 60); + var remainingSeconds = seconds % 60; + return pad(minutes) + ":" + pad(remainingSeconds); +} + +// Helper function to pad single digits with leading zero +function pad(num) { + return num < 10 ? "0" + num : num; +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..ff2d3b453 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm clock app