From ed6a4c0f0d86f854eddc7ab34e9ff7331dcf1a1c Mon Sep 17 00:00:00 2001 From: Abrsh100 Date: Sat, 15 Nov 2025 07:51:59 +0000 Subject: [PATCH 1/2] modifing html and js --- Sprint-3/alarmclock/alarmclock.js | 51 ++++++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 4 +-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..75bdb4cfa 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,52 @@ -function setAlarm() {} +let countdownInterval + +function setAlarm() { + const alarmTime = document.getElementById("alarmSet").value; + if (!alarmTime) { + alert("Please select a time for the alarm."); + return; + } + const alarmDate = new Date(alarmTime); + const now = new Date(); + + const timeToAlarm = alarmDate.getTime() - now.getTime(); + if (isNaN(timeToAlarm)) { + alert("Invalid date format. Please select a valid date and time."); + return; + } else if (timeToAlarm <= 0) { + alert("Please select a future time for the alarm."); + return; + } + clearInterval(countdownInterval); + setTimeout(() => { + playAlarm(); + }, timeToAlarm); + alert("Alarm set for " + alarmDate.toString()); + + + const updateCountdown = () => { + const now = new Date(); + const timeRemaining = alarmDate.getTime() - now.getTime(); + + if (timeRemaining <= 0) { + clearInterval(countdownInterval); + document.getElementById("timeRemaining").innerText = "Time Remaining: 00:00"; + return; + } + + const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000); + + document.getElementById("timeRemaining").innerText = + "Time Remaining: " + + String(minutes).padStart(2, '0') + + ":" + + String(seconds).padStart(2, '0'); + }; + updateCountdown(); + countdownInterval = setInterval(updateCountdown, 1000); +}; + // DO NOT EDIT BELOW HERE @@ -23,3 +71,4 @@ function pauseAlarm() { } window.onload = setup; + diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..51239005c 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,13 +4,13 @@ - Title here + Alarm Clock

Time Remaining: 00:00

- + From 5de1d20118fa46261639c00ce78bc2f9a4c00196 Mon Sep 17 00:00:00 2001 From: Abrsh100 Date: Wed, 26 Nov 2025 08:20:30 +0000 Subject: [PATCH 2/2] change the date input in to seconds --- Sprint-3/alarmclock/alarmclock.js | 62 +++++++++++++++---------------- Sprint-3/alarmclock/index.html | 2 +- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 75bdb4cfa..f96df8e9a 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,51 +1,49 @@ let countdownInterval +let alarmTimeout; function setAlarm() { - const alarmTime = document.getElementById("alarmSet").value; - if (!alarmTime) { - alert("Please select a time for the alarm."); + const secondsInput = document.getElementById("alarmSet").value; + const totalSeconds = parseInt(secondsInput, 10); + + if (isNaN(totalSeconds) || totalSeconds <= 0) { + alert("Please enter a valid number of seconds."); return; } - const alarmDate = new Date(alarmTime); - const now = new Date(); - const timeToAlarm = alarmDate.getTime() - now.getTime(); - if (isNaN(timeToAlarm)) { - alert("Invalid date format. Please select a valid date and time."); - return; - } else if (timeToAlarm <= 0) { - alert("Please select a future time for the alarm."); - return; - } + // Clear any existing countdown clearInterval(countdownInterval); - setTimeout(() => { - playAlarm(); - }, timeToAlarm); - alert("Alarm set for " + alarmDate.toString()); - + clearTimeout(alarmTimeout); + + const alarmDuration = totalSeconds * 1000; + const endTime = Date.now() + alarmDuration; - const updateCountdown = () => { - const now = new Date(); - const timeRemaining = alarmDate.getTime() - now.getTime(); + // set alarm + alarmTimeout = setTimeout(() => { + playAlarm(); + }, alarmDuration); - if (timeRemaining <= 0) { + // Update countdown + const updateCountdown = () => { + const timeLeft = endTime - Date.now(); + + if (timeLeft <= 0) { clearInterval(countdownInterval); document.getElementById("timeRemaining").innerText = "Time Remaining: 00:00"; return; } + + const minutes = Math.floor((timeLeft / 1000) / 60); + const seconds = Math.floor((timeLeft / 1000) % 60); - const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60)); - const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000); - - document.getElementById("timeRemaining").innerText = - "Time Remaining: " + - String(minutes).padStart(2, '0') + - ":" + - String(seconds).padStart(2, '0'); - }; + document.getElementById("timeRemaining").innerText = + `Time Remaining: ${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`; + }; + + //start countdown updateCountdown(); countdownInterval = setInterval(updateCountdown, 1000); -}; +} + // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 51239005c..477ca4eeb 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -10,7 +10,7 @@

Time Remaining: 00:00

- +