diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..fe41a429b 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,51 @@ -function setAlarm() {} +let alarmInterval; +let timeRemaining = 0; + +function setAlarm() { + // Get the input value + const alarmInput = document.getElementById("alarmSet"); + const seconds = parseInt(alarmInput.value); + + // Validate input + if (isNaN(seconds) || seconds <= 0) { + alert("Please enter a valid number of seconds!"); + return; + } + + // Clear any existing interval + if (alarmInterval) { + clearInterval(alarmInterval); + } + + // Set the time remaining + timeRemaining = seconds; + + // Update the display immediately + updateDisplay(); + + // Start the countdown + alarmInterval = setInterval(() => { + timeRemaining--; + updateDisplay(); + + // Check if time is up + if (timeRemaining <= 0) { + clearInterval(alarmInterval); + playAlarm(); + } + }, 1000); +} + +function updateDisplay() { + const minutes = Math.floor(timeRemaining / 60); + const seconds = timeRemaining % 60; + + // Format with leading zeros + const formattedTime = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + + // Update the display + document.getElementById("timeRemaining").textContent = `Time Remaining: ${formattedTime}`; +} // 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