From bc170ad300511e9954648583291a0112c60e667d Mon Sep 17 00:00:00 2001 From: Ayodeji Ayorinde Date: Wed, 25 Mar 2026 08:10:32 +0000 Subject: [PATCH] Completed alarm clock task --- Sprint-3/alarmclock/alarmclock.js | 47 ++++++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 37 +++++++++++++----------- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..5bec88187 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,49 @@ -function setAlarm() {} +//function setAlarm() {} + +let countdownInterval; + +function setAlarm() { + // 1. Get the input value and the display element + const alarmInput = document.getElementById("alarmSet").value; + const timeRemainingDisplay = document.getElementById("timeRemaining"); + + // Safety check: Ensure input is a number and greater than 0 + if (!alarmInput || alarmInput <= 0) return; + + let timeRemaining = parseInt(alarmInput); + + // 2. Clear any existing timers to prevent "speeding up" if clicked multiple times + clearInterval(countdownInterval); + + // 3. Initial update so the user sees the time immediately + updateDisplay(timeRemaining, timeRemainingDisplay); + + // 4. Start the countdown logic + countdownInterval = setInterval(() => { + timeRemaining -= 1; + updateDisplay(timeRemaining, timeRemainingDisplay); + + // 5. Trigger the alarm and stop the counter when 0 is reached + if (timeRemaining <= 0) { + clearInterval(countdownInterval); + playAlarm(); + } + }, 1000); +} + +/** + * Helper function to format the time as 00:00 + */ +function updateDisplay(totalSeconds, displayElement) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + // Converts numbers to strings and pads with a leading zero if needed + const formattedMinutes = minutes.toString().padStart(2, "0"); + const formattedSeconds = seconds.toString().padStart(2, "0"); + + displayElement.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..36715d7d9 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,20 +1,23 @@ - - - - - Title here - - -
-

Time Remaining: 00:00

- - - - -
- - - + + + + + Alarm clock app + + + +
+

Time Remaining: 00:00

+ + + + + +
+ + + + \ No newline at end of file