From 80ba7b40bf12f68520f0cac7d7b7520e4a15506d Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 30 Oct 2025 13:52:05 +0000 Subject: [PATCH 1/2] building alarmclock --- Sprint-3/alarmclock/alarmclock.js | 35 ++++++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 4 ++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..d890ef9c6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,37 @@ -function setAlarm() {} +let countdownInterval; + +function setAlarm() { + const input = document.getElementById("alarmSet"); + let seconds = Number(input.value); + + updateAlarmDisplay(seconds); + + // Clear any existing interval + clearInterval(countdownInterval); + + countdownInterval = window.setInterval(() => { + seconds--; + if (seconds <= 0) { + clearInterval(countdownInterval); + updateAlarmDisplay(0); + playAlarm(); + } else { + updateAlarmDisplay(seconds); + } + }, 1000); +} + +function updateAlarmDisplay(seconds) { + const display = document.getElementById("timeRemaining"); + const minutes = Math.floor(seconds / 60) + .toString() + .padStart(2, "0"); + const secs = (seconds % 60).toString().padStart(2, "0"); + display.textContent = `Time Remaining: ${minutes}:${secs}`; +} + +window.setAlarm = setAlarm; +window.updateAlarmDisplay = updateAlarmDisplay; // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..e6c8e4f34 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - +< - Title here + alarmclock
From 3724883083f8e7f24832e7e2efb9b7189911094c Mon Sep 17 00:00:00 2001 From: AdnaanA Date: Thu, 30 Oct 2025 14:24:29 +0000 Subject: [PATCH 2/2] Added validation and colour changes to when alarm starts and ends --- Sprint-3/alarmclock/alarmclock.js | 28 +++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index d890ef9c6..feb4aeac3 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -4,8 +4,17 @@ function setAlarm() { const input = document.getElementById("alarmSet"); let seconds = Number(input.value); + // Validation: Check if the input is a valid positive number + if (isNaN(seconds) || seconds <= 0) { + alert("Please enter a valid positive number for seconds."); + return; // Stop the function if the input is invalid + } + updateAlarmDisplay(seconds); + // Change the background color to green when the timer starts + document.body.style.backgroundColor = "green"; + // Clear any existing interval clearInterval(countdownInterval); @@ -14,7 +23,9 @@ function setAlarm() { if (seconds <= 0) { clearInterval(countdownInterval); updateAlarmDisplay(0); - playAlarm(); + // Change the background color to red when alarm hits zero + document.body.style.backgroundColor = "red"; + playAlarm(); // Assuming you have a playAlarm function defined elsewhere } else { updateAlarmDisplay(seconds); } @@ -30,8 +41,23 @@ function updateAlarmDisplay(seconds) { display.textContent = `Time Remaining: ${minutes}:${secs}`; } +function stopAlarm() { + // Clear the countdown interval + clearInterval(countdownInterval); + countdownInterval = null; + + // Reset the background color and the displayed time + document.body.style.backgroundColor = ""; + updateAlarmDisplay(0); // Reset to 00:00 +} + +// Add event listeners to buttons +document.getElementById("set").addEventListener("click", setAlarm); +document.getElementById("stop").addEventListener("click", stopAlarm); + window.setAlarm = setAlarm; window.updateAlarmDisplay = updateAlarmDisplay; +window.stopAlarm = stopAlarm; // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index e6c8e4f34..f216bb2a0 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,4 +1,4 @@ -< +