diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..1adde3ff6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,48 @@ -function setAlarm() {} +let currentTimerId = null; + +function setAlarm() { + const input = document.getElementById("alarmSet"); + const heading = document.getElementById("timeRemaining"); + let seconds = Number(input.value); + + // Sanitise input + if (!Number.isInteger(seconds) || seconds <= 0) { + alert("Please enter a positive whole number"); + return; + } + + // Reset before starting new countdown + if (currentTimerId !== null) { + clearInterval(currentTimerId); + } + + pauseAlarm(); + + document.body.style.backgroundColor = ""; + + function updateDisplay(remainingSeconds) { + const minutes = Math.floor(remainingSeconds / 60); + const secs = remainingSeconds % 60; + const formattedTime = `${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; + heading.textContent = `Time Remaining: ${formattedTime}`; + } + + // Set initial display + updateDisplay(seconds); + + // Start countdown + currentTimerId = setInterval(() => { + seconds--; + updateDisplay(seconds); + + if (seconds <= 0) { + clearInterval(currentTimerId); + currentTimerId = null; + playAlarm(); + document.body.style.backgroundColor = "red"; + } + }, 1000); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..8b3ff248e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,16 +1,17 @@ - + - Title here + + Alarm clock app

Time Remaining: 00:00

- +