From 76cb8e70523f9c540e6e3d6523e75395f94d9319 Mon Sep 17 00:00:00 2001 From: Enice-Codes Date: Fri, 7 Aug 2026 00:46:08 +0200 Subject: [PATCH 1/3] completed the alarm requirements --- Sprint-3/alarmclock/alarmclock.js | 35 ++++++++++++++++++++++++++++--- Sprint-3/alarmclock/index.html | 2 +- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..aa0278534 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,7 +1,36 @@ -function setAlarm() {} +let timer = null; -// DO NOT EDIT BELOW HERE +function formatTime(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`; +} + +function updateDisplay(seconds) { + document.getElementById("timeRemaining").textContent = + `Time Remaining: ${formatTime(seconds)}`; +} + +function setAlarm() { + if (timer) { + clearInterval(timer); + } + let seconds = Number(document.getElementById("alarmSet").value); + updateDisplay(seconds); + + timer = setInterval(() => { + seconds--; + updateDisplay(seconds); + + if (seconds <= 0) { + clearInterval(timer); + playAlarm(); + } + }, 1000); +} + +// DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); function setup() { @@ -22,4 +51,4 @@ function pauseAlarm() { audio.pause(); } -window.onload = setup; +window.onload = setup; \ No newline at end of file diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..2c403ab6e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm Clock app
From 9027349cbeff248f1098a34e3d15a4c40491b944 Mon Sep 17 00:00:00 2001 From: Enice-Codes Date: Sat, 15 Aug 2026 11:46:36 +0200 Subject: [PATCH 2/3] added negatice intervals and positve intervals to ensure the alarm only works with positive intervals --- Sprint-3/alarmclock/alarmclock.js | 38 ++++++++++++++++++++++---- Sprint-3/alarmclock/alarmclock.test.js | 3 ++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index aa0278534..7962be1c6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,5 @@ let timer = null; +let secondsRemaining = 0; function formatTime(totalSeconds) { const minutes = Math.floor(totalSeconds / 60); @@ -14,22 +15,49 @@ function updateDisplay(seconds) { function setAlarm() { if (timer) { clearInterval(timer); + timer = null; } - let seconds = Number(document.getElementById("alarmSet").value); - updateDisplay(seconds); + const inputValue = document.getElementById("alarmSet").value; + const seconds = Number(inputValue); + + if ( + inputValue === "" || + Number.isNaN(seconds) || + !Number.isInteger(seconds) || + seconds <= 0 + ) { + document.getElementById("timeRemaining").textContent = + "Please enter a whole number greater than 0."; + return; + } + + secondsRemaining = seconds; + updateDisplay(secondsRemaining); timer = setInterval(() => { - seconds--; - updateDisplay(seconds); + secondsRemaining--; + updateDisplay(secondsRemaining); - if (seconds <= 0) { + if (secondsRemaining <= 0) { clearInterval(timer); + timer = null; playAlarm(); } }, 1000); } +function stopTimer() { + if (timer) { + clearInterval(timer); + timer = null; + } +} + +document.addEventListener("DOMContentLoaded", () => { + document.getElementById("stop").addEventListener("click", stopTimer); +}); + // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); diff --git a/Sprint-3/alarmclock/alarmclock.test.js b/Sprint-3/alarmclock/alarmclock.test.js index 85b7356dc..bfcd54142 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -2,6 +2,7 @@ There are some Tests in this file that will help you work out if your code is working. */ + const path = require("path"); const { JSDOM } = require("jsdom"); @@ -15,6 +16,8 @@ beforeEach(async () => { jest.useFakeTimers(); + + // do this so students can use element.innerText which jsdom does not implement Object.defineProperty(page.window.HTMLElement.prototype, "innerText", { get() { From 7de5ee63619c2931f903fbcc5e4dc5cfcd8c0df2 Mon Sep 17 00:00:00 2001 From: Enice-Codes Date: Thu, 20 Aug 2026 02:41:08 +0200 Subject: [PATCH 3/3] modified work --- Sprint-3/alarmclock/alarmclock.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 7962be1c6..385a81e1a 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,6 +1,10 @@ let timer = null; let secondsRemaining = 0; +const timeRemainingEl = document.getElementById("timeRemaining"); +const alarmSetEl = document.getElementById("alarmSet"); +const stopButtonEl = document.getElementById("stop"); + function formatTime(totalSeconds) { const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; @@ -8,8 +12,7 @@ function formatTime(totalSeconds) { } function updateDisplay(seconds) { - document.getElementById("timeRemaining").textContent = - `Time Remaining: ${formatTime(seconds)}`; + timeRemainingEl.textContent = `Time Remaining: ${formatTime(seconds)}`; } function setAlarm() { @@ -18,7 +21,7 @@ function setAlarm() { timer = null; } - const inputValue = document.getElementById("alarmSet").value; + const inputValue = alarmSetEl.value; const seconds = Number(inputValue); if ( @@ -27,8 +30,7 @@ function setAlarm() { !Number.isInteger(seconds) || seconds <= 0 ) { - document.getElementById("timeRemaining").textContent = - "Please enter a whole number greater than 0."; + timeRemainingEl.textContent = "Please enter a whole number greater than 0."; return; } @@ -54,9 +56,7 @@ function stopTimer() { } } -document.addEventListener("DOMContentLoaded", () => { - document.getElementById("stop").addEventListener("click", stopTimer); -}); +stopButtonEl.addEventListener("click", stopTimer); // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3");