diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..9dc7d4d51 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,53 @@ -function setAlarm() {} +const input = document.getElementById("alarmSet"); +const timeDisplay = document.querySelector("span"); + +let alarmInterval = null; + +function setAlarm() { + // sanitize input: convert to number, floor to integer seconds, clamp to >= 0 + let timeInput = Math.floor(Number(input.value) || 0); + if (timeInput < 0) timeInput = 0; + + // clear any existing interval so multiple clicks don't create multiple timers + if (alarmInterval) { + clearInterval(alarmInterval); + alarmInterval = null; + } + + // handle negative input explicitly + const rawValue = Number(input.value); + if (rawValue < 0) { + timeDisplay.textContent = "Invalid value"; + return; + } + + let minutes = Math.floor(timeInput / 60); + let seconds = timeInput % 60; + + minutes = minutes.toString().padStart(2, "0"); + seconds = seconds.toString().padStart(2, "0"); + timeDisplay.textContent = `${minutes}:${seconds}`; + + // if the input is 0, trigger the alarm immediately + if (timeInput <= 0) { + playAlarm(); + return; + } + + alarmInterval = setInterval(() => { + timeInput--; + let remainingMinutes = Math.floor(timeInput / 60); + let remainingSeconds = timeInput % 60; + remainingMinutes = remainingMinutes.toString().padStart(2, "0"); + remainingSeconds = remainingSeconds.toString().padStart(2, "0"); + timeDisplay.textContent = `${remainingMinutes}:${remainingSeconds}`; + if (timeInput <= 0) { + playAlarm(); + clearInterval(alarmInterval); + alarmInterval = null; + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -20,6 +69,11 @@ function playAlarm() { function pauseAlarm() { audio.pause(); + audio.currentTime = 0; + if (alarmInterval) { + clearInterval(alarmInterval); + alarmInterval = null; + } } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..6e5d06868 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,20 +1,19 @@ - - - - - Title here - - -
-

Time Remaining: 00:00

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

Time Remaining: 00:00

+ + + + +
+ +