From 3658ef438438c42cb980471324436b01baced0e4 Mon Sep 17 00:00:00 2001 From: Carol Date: Sun, 22 Nov 2026 23:45:31 +0000 Subject: [PATCH] updated working solution for the alarm clock app --- Sprint-3/alarmclock/alarmclock.js | 51 ++++++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 15 +++++++-- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b1..60f6eb072b 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,51 @@ -function setAlarm() {} +let countdownInterval = null; + +function setAlarm() { + const timeDisplay = document.getElementById("timeRemaining"); + const alarmInput = document.getElementById("alarmSet"); + const inputValue = alarmInput.value.trim(); + + if (inputValue === "") { + return; + } + + let secondsRemaining = Number(inputValue); + + if (!Number.isFinite(secondsRemaining) || secondsRemaining < 0) { + return; + } + + if (countdownInterval !== null) { + window.clearInterval(countdownInterval); + countdownInterval = null; + } + + const updateDisplay = () => { + const minutes = Math.floor(secondsRemaining / 60) + .toString() + .padStart(2, "0"); + const seconds = (secondsRemaining % 60).toString().padStart(2, "0"); + timeDisplay.textContent = `Time Remaining: ${minutes}:${seconds}`; + }; + + updateDisplay(); + + if (secondsRemaining === 0) { + playAlarm(); + return; + } + + countdownInterval = window.setInterval(() => { + secondsRemaining -= 1; + updateDisplay(); + + if (secondsRemaining === 0) { + window.clearInterval(countdownInterval); + countdownInterval = null; + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -15,11 +62,13 @@ function setup() { } function playAlarm() { + document.body.classList.add("alarm-active"); audio.play(); } function pauseAlarm() { audio.pause(); + document.body.classList.remove("alarm-active"); } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d99..e541ba5d20 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,19 @@ - Title here + Alarm clock app + +
@@ -15,6 +27,5 @@

Time Remaining: 00:00

-