From 3a1925a480744b66f4270196167a5128652e899c Mon Sep 17 00:00:00 2001 From: Vitalii Kmit Date: Sat, 1 Aug 2026 08:59:16 +0100 Subject: [PATCH 1/4] Set up looping alarm --- Sprint-3/alarmclock/alarmclock.js | 28 +++++++++++++++++++++++++++- Sprint-3/alarmclock/index.html | 8 ++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..1611f595e 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,30 @@ -function setAlarm() {} +const oneSecondInMilliseconds = 1000; +const lengthOfAlarmSound = 20000; + +function setAlarm() { + // set up time + let now = Date.now(); + let timeLeft = document.getElementById("alarmSet").value; + + // validate result + const result = parseInt(timeLeft); + if (isNaN(result)) { + return; + } + + setTimeout(alarmFinished, timeLeft * oneSecondInMilliseconds); + audio.loop = true; + playAlarm(); +} + +function stopAlarm() { + pauseAlarm(); +} + +function alarmFinished() { + console.log("Alarm finished"); + stopAlarm(); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..77e8f5c77 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - + - Title here + Alarm clock app
@@ -12,8 +12,8 @@

Time Remaining: 00:00

- - + +
From d79612d9b4153188470da4b60877df3129172ba4 Mon Sep 17 00:00:00 2001 From: Vitalii Kmit Date: Sat, 1 Aug 2026 19:08:39 +0100 Subject: [PATCH 2/4] Revert the previous change for index.html --- Sprint-3/alarmclock/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 77e8f5c77..66748001e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -12,8 +12,8 @@

Time Remaining: 00:00

- - + + From 1e6f60feb69431cea113bbc2e44608e87e5438a6 Mon Sep 17 00:00:00 2001 From: Vitalii Kmit Date: Sat, 1 Aug 2026 19:09:10 +0100 Subject: [PATCH 3/4] Add setTimout and setInterval --- Sprint-3/alarmclock/alarmclock.js | 61 ++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 1611f595e..4e2a247a0 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,10 +1,20 @@ const oneSecondInMilliseconds = 1000; const lengthOfAlarmSound = 20000; +let timeLeft; +let intervalID; +let timeoutID; +let display; + +// document.getElementById("set").addEventListener("click", setAlarm); +// document.getElementById("stop").addEventListener("click", stopAlarm); + function setAlarm() { + stopAlarm(); + // set up time - let now = Date.now(); - let timeLeft = document.getElementById("alarmSet").value; + timeLeft = document.getElementById("alarmSet").value; + display = document.getElementById("timeRemaining"); // validate result const result = parseInt(timeLeft); @@ -12,18 +22,57 @@ function setAlarm() { return; } - setTimeout(alarmFinished, timeLeft * oneSecondInMilliseconds); - audio.loop = true; - playAlarm(); + display.textContent = formatTime(timeLeft); + + if (!timeoutID) { + timeoutID = setTimeout(alarmFinished, timeLeft * oneSecondInMilliseconds); + } + + if (!intervalID) { + intervalID = setInterval(updateTimer, 1000); + } + + console.log(`Alarm set for ${timeLeft} seconds`); +} + +function updateTimer() { + timeLeft = timeLeft - 1; + + display.textContent = formatTime(timeLeft); + + if (timeLeft <= 0) { + cleanUp(); + } +} + +// give a time in seconds +function formatTime(seconds) { + minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + + const seconds_padded = String(seconds).padStart(2, "0"); + const minutes_paddded = String(minutes).padStart(2, "0"); + + const formatted_seconds = `Time Remaining: ${minutes_paddded}:${seconds_padded}`; + return formatted_seconds; } function stopAlarm() { pauseAlarm(); + cleanUp(); +} + +function cleanUp() { + clearInterval(intervalID); + clearTimeout(timeoutID); + timeoutID = null; + intervalID = null; } function alarmFinished() { + audio.loop = true; + playAlarm(); console.log("Alarm finished"); - stopAlarm(); } // DO NOT EDIT BELOW HERE From a2d19808fd39fcf25cf771d602740a1b676d3992 Mon Sep 17 00:00:00 2001 From: Vitalii Kmit Date: Mon, 3 Aug 2026 13:45:02 +0100 Subject: [PATCH 4/4] Remove setTimout to pass all tests --- Sprint-3/alarmclock/alarmclock.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 4e2a247a0..44f909551 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -6,9 +6,6 @@ let intervalID; let timeoutID; let display; -// document.getElementById("set").addEventListener("click", setAlarm); -// document.getElementById("stop").addEventListener("click", stopAlarm); - function setAlarm() { stopAlarm(); @@ -24,10 +21,6 @@ function setAlarm() { display.textContent = formatTime(timeLeft); - if (!timeoutID) { - timeoutID = setTimeout(alarmFinished, timeLeft * oneSecondInMilliseconds); - } - if (!intervalID) { intervalID = setInterval(updateTimer, 1000); } @@ -41,6 +34,7 @@ function updateTimer() { display.textContent = formatTime(timeLeft); if (timeLeft <= 0) { + alarmFinished(); cleanUp(); } }