From f9976e9bf9cb17439c011712e43f87691dfa05bb Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:20:48 +0100 Subject: [PATCH 1/3] Add alarm countdown feature to setAlarm function Implemented alarm countdown functionality with display update. --- Sprint-3/alarmclock/alarmclock.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..fed27ad9e 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,28 @@ -function setAlarm() {} +const input = document.getElementById("alarmSet"); +const timeDisplay = document.querySelector("span"); + +function setAlarm() { + let timeInput = Number(input.value); + let minutes = Math.floor(timeInput / 60); + let seconds = timeInput % 60; + + minutes = minutes.toString().padStart(2, "0"); + seconds = seconds.toString().padStart(2, "0"); + timeDisplay.textContent = `${minutes}:${seconds}`; + + const alarm = setInterval(() => { + timeInput--; + let remainingMinutes = Math.floor(timeInput / 60); + let remainingSeconds = timeInput % 60; + remainingMinutes = remainingMinutes.toString().padStart(2, "0"); + remainingSeconds = remainingSeconds.toString().padStart(2, "0"); + timeDisplay.textContent = `${remainingMinutes}:${remainingSeconds}`; + if (timeInput === 0) { + playAlarm(); + clearInterval(alarm); + } + }, 1000); +} // DO NOT EDIT BELOW HERE From d7c36382f2ebf224091c3155570dc9ae7b482f5e Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 6 Aug 2026 19:21:22 +0100 Subject: [PATCH 2/3] Refactor index.html for alarm clock app Updated the HTML structure and title for the alarm clock app. --- Sprint-3/alarmclock/index.html | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..6e5d06868 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,20 +1,19 @@ - - - - - Title here - - -
-

Time Remaining: 00:00

- - - - - -
- - + + + + + Alarm clock app + + +
+

Time Remaining: 00:00

+ + + + +
+ + From 6fe766a361100ae86ca94f025230e696fcf86dd4 Mon Sep 17 00:00:00 2001 From: anitahy73 Date: Thu, 13 Aug 2026 00:04:15 +0100 Subject: [PATCH 3/3] Improve alarm input validation and handling Sanitize input and handle negative values in alarm clock. --- Sprint-3/alarmclock/alarmclock.js | 38 +++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index fed27ad9e..9dc7d4d51 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,8 +1,26 @@ const input = document.getElementById("alarmSet"); const timeDisplay = document.querySelector("span"); +let alarmInterval = null; + function setAlarm() { - let timeInput = Number(input.value); + // sanitize input: convert to number, floor to integer seconds, clamp to >= 0 + let timeInput = Math.floor(Number(input.value) || 0); + if (timeInput < 0) timeInput = 0; + + // clear any existing interval so multiple clicks don't create multiple timers + if (alarmInterval) { + clearInterval(alarmInterval); + alarmInterval = null; + } + + // handle negative input explicitly + const rawValue = Number(input.value); + if (rawValue < 0) { + timeDisplay.textContent = "Invalid value"; + return; + } + let minutes = Math.floor(timeInput / 60); let seconds = timeInput % 60; @@ -10,16 +28,23 @@ function setAlarm() { seconds = seconds.toString().padStart(2, "0"); timeDisplay.textContent = `${minutes}:${seconds}`; - const alarm = setInterval(() => { + // if the input is 0, trigger the alarm immediately + if (timeInput <= 0) { + playAlarm(); + return; + } + + alarmInterval = setInterval(() => { timeInput--; let remainingMinutes = Math.floor(timeInput / 60); let remainingSeconds = timeInput % 60; remainingMinutes = remainingMinutes.toString().padStart(2, "0"); remainingSeconds = remainingSeconds.toString().padStart(2, "0"); timeDisplay.textContent = `${remainingMinutes}:${remainingSeconds}`; - if (timeInput === 0) { + if (timeInput <= 0) { playAlarm(); - clearInterval(alarm); + clearInterval(alarmInterval); + alarmInterval = null; } }, 1000); } @@ -44,6 +69,11 @@ function playAlarm() { function pauseAlarm() { audio.pause(); + audio.currentTime = 0; + if (alarmInterval) { + clearInterval(alarmInterval); + alarmInterval = null; + } } window.onload = setup;