From 35d8c2fd5eaded9293756f2762ec18e9a8789693 Mon Sep 17 00:00:00 2001 From: Alina Sofragiu Date: Sun, 19 Jul 2026 11:44:27 +0100 Subject: [PATCH] alarm clock --- Sprint-3/alarmclock/alarmclock.js | 44 ++++++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 4 +-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..62f89b465 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,46 @@ -function setAlarm() {} +let timer = null; + +function updateHeading(secondsRemaining) { + const minutes = Math.floor(secondsRemaining / 60); + const seconds = secondsRemaining % 60; + + const formattedMinutes = String(minutes).padStart(2, "0"); + const formattedSeconds = String(seconds).padStart(2, "0"); + + document.getElementById("timeRemaining").innerText = + `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; +} + +function setAlarm() { + if (timer) { + clearInterval(timer); + } + + let secondsRemaining = Number(document.getElementById("alarmSet").value); + + updateHeading(secondsRemaining); + + timer = setInterval(() => { + secondsRemaining--; + + if (secondsRemaining >= 0) { + updateHeading(secondsRemaining); + } + + if (secondsRemaining === 0) { + playAlarm(); + clearInterval(timer); + + // Optional extra task - flash the background + let flash = false; + + setInterval(() => { + document.body.style.backgroundColor = flash ? "white" : "red"; + flash = !flash; + }, 500); + } + }, 1000); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..66748001e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - + - Title here + Alarm clock app