From 75028ecdac54da40ee1eac7846e2ba3b5ef144a0 Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Sat, 4 Apr 2026 18:51:12 +0100 Subject: [PATCH 1/6] I have completed sprint 3 alarm clock --- Sprint-3/alarmclock/alarmclock.js | 90 +++++++++++++++++++++++++++---- Sprint-3/alarmclock/index.html | 21 ++++---- Sprint-3/alarmclock/style.css | 16 +++--- 3 files changed, 95 insertions(+), 32 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..885caffac 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,93 @@ -function setAlarm() {} +let timeRemaining = 0; +let timerId = null; -// DO NOT EDIT BELOW HERE +const audio = new Audio("alarmsound.mp3"); -var audio = new Audio("alarmsound.mp3"); +// FORMAT mm:ss +function formatTime(seconds) { + const mins = Math.floor(seconds / 60); + const secs = seconds % 60; -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); - }); + return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; +} - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); +// UPDATE DISPLAY (MUST MATCH TEST EXACTLY) +function updateDisplay() { + const heading = document.getElementById("timeRemaining"); + heading.textContent = "Time Remaining: " + formatTime(timeRemaining); } +// REQUIRED BY TESTS function playAlarm() { + audio.loop = true; + audio.currentTime = 0; audio.play(); } -function pauseAlarm() { +// STOP ALARM +function stopAlarm() { audio.pause(); + audio.currentTime = 0; + audio.loop = false; + + clearInterval(timerId); + timerId = null; + + document.body.style.backgroundColor = ""; +} + +// TRIGGER ALARM +function triggerAlarm() { + document.body.style.backgroundColor = "red"; + playAlarm(); +} + +// START TIMER +function startTimer() { + clearInterval(timerId); + + timerId = setInterval(() => { + timeRemaining--; + + updateDisplay(); + + if (timeRemaining <= 0) { + clearInterval(timerId); + timerId = null; + + timeRemaining = 0; + updateDisplay(); + + triggerAlarm(); + } + }, 1000); +} + +// SET ALARM +function setAlarm() { + const input = document.getElementById("alarmSet").value; + + // ensure clean number input + timeRemaining = parseInt(input, 10); + + if (isNaN(timeRemaining) || timeRemaining < 0) { + timeRemaining = 0; + } + + updateDisplay(); + + if (timeRemaining > 0) { + startTimer(); + } +} + +// SETUP +function setup() { + document.getElementById("set").addEventListener("click", setAlarm); + document.getElementById("stop").addEventListener("click", stopAlarm); + + timeRemaining = 0; + updateDisplay(); } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..2025e301b 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -2,19 +2,18 @@ - - - Title here + Alarm Clock + -
-

Time Remaining: 00:00

- - +

+ Time Remaining: 00:00 +

+ + + + - - -
- + \ No newline at end of file diff --git a/Sprint-3/alarmclock/style.css b/Sprint-3/alarmclock/style.css index 0c72de38b..41ebdc519 100644 --- a/Sprint-3/alarmclock/style.css +++ b/Sprint-3/alarmclock/style.css @@ -1,15 +1,11 @@ -.centre { - position: fixed; - top: 50%; - left: 50%; - -webkit-transform: translate(-50%, -50%); - transform: translate(-50%, -50%); -} - -#alarmSet { - margin: 20px; +body { + background-color: blanchedalmond; } h1 { text-align: center; } + +#alarmSet { + margin: 20px; +} \ No newline at end of file From e53ac5d26b7e68e16e649d8ccadcfa2de9b81845 Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Wed, 8 Apr 2026 17:23:58 +0100 Subject: [PATCH 2/6] fix --- Sprint-3/alarmclock/alarmclock.js | 12 ++++++------ Sprint-3/alarmclock/index.html | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 885caffac..70fb4841a 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,4 @@ -let timeRemaining = 0; + let timerId = null; const audio = new Audio("alarmsound.mp3"); @@ -12,7 +12,7 @@ function formatTime(seconds) { } // UPDATE DISPLAY (MUST MATCH TEST EXACTLY) -function updateDisplay() { +function updateDisplay(timeRemaining) { const heading = document.getElementById("timeRemaining"); heading.textContent = "Time Remaining: " + formatTime(timeRemaining); } @@ -43,7 +43,7 @@ function triggerAlarm() { } // START TIMER -function startTimer() { +function startTimer(timeRemaining) { clearInterval(timerId); timerId = setInterval(() => { @@ -68,7 +68,7 @@ function setAlarm() { const input = document.getElementById("alarmSet").value; // ensure clean number input - timeRemaining = parseInt(input, 10); +let timeRemaining = parseInt(input, 10); if (isNaN(timeRemaining) || timeRemaining < 0) { timeRemaining = 0; @@ -77,11 +77,11 @@ function setAlarm() { updateDisplay(); if (timeRemaining > 0) { - startTimer(); + startTimer(timeRemaining); } } -// SETUP +// SETUP function setup() { document.getElementById("set").addEventListener("click", setAlarm); document.getElementById("stop").addEventListener("click", stopAlarm); diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 2025e301b..6d5fc80f1 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -14,6 +14,6 @@

- + \ No newline at end of file From cd26977202fd605cbefc9185603865dd9ba71bed Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Thu, 16 Apr 2026 18:07:33 +0100 Subject: [PATCH 3/6] Update alarm.clock.js based on feedback . --- Sprint-3/alarmclock/alarmclock.js | 43 ++++++++++++++----------------- Sprint-3/alarmclock/style.css | 3 +++ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 70fb4841a..53fdd8091 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,4 @@ - +let timeRemaining = 0; let timerId = null; const audio = new Audio("alarmsound.mp3"); @@ -11,10 +11,10 @@ function formatTime(seconds) { return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; } -// UPDATE DISPLAY (MUST MATCH TEST EXACTLY) -function updateDisplay(timeRemaining) { +// UPDATE DISPLAY +function updateDisplay(time) { const heading = document.getElementById("timeRemaining"); - heading.textContent = "Time Remaining: " + formatTime(timeRemaining); + heading.textContent = "Time Remaining: " + formatTime(time); } // REQUIRED BY TESTS @@ -24,7 +24,7 @@ function playAlarm() { audio.play(); } -// STOP ALARM +// STOP / RESET ALARM STATE function stopAlarm() { audio.pause(); audio.currentTime = 0; @@ -33,30 +33,30 @@ function stopAlarm() { clearInterval(timerId); timerId = null; - document.body.style.backgroundColor = ""; + document.body.classList.toggle("alarm-activated", false); } // TRIGGER ALARM function triggerAlarm() { - document.body.style.backgroundColor = "red"; + document.body.classList.toggle("alarm-activated", true); playAlarm(); } // START TIMER -function startTimer(timeRemaining) { - clearInterval(timerId); +function startTimer() { + stopAlarm(); timerId = setInterval(() => { timeRemaining--; - updateDisplay(); + updateDisplay(timeRemaining); if (timeRemaining <= 0) { clearInterval(timerId); timerId = null; timeRemaining = 0; - updateDisplay(); + updateDisplay(timeRemaining); triggerAlarm(); } @@ -66,28 +66,25 @@ function startTimer(timeRemaining) { // SET ALARM function setAlarm() { const input = document.getElementById("alarmSet").value; + timeRemaining = parseInt(input, 10); - // ensure clean number input -let timeRemaining = parseInt(input, 10); - - if (isNaN(timeRemaining) || timeRemaining < 0) { + if (isNaN(timeRemaining) || timeRemaining <= 0) { + stopAlarm(); timeRemaining = 0; + updateDisplay(timeRemaining); + return; } - updateDisplay(); - - if (timeRemaining > 0) { - startTimer(timeRemaining); - } + updateDisplay(timeRemaining); + startTimer(); } -// SETUP +// SETUP function setup() { document.getElementById("set").addEventListener("click", setAlarm); document.getElementById("stop").addEventListener("click", stopAlarm); - timeRemaining = 0; - updateDisplay(); + updateDisplay(0); } window.onload = setup; diff --git a/Sprint-3/alarmclock/style.css b/Sprint-3/alarmclock/style.css index 41ebdc519..468f62193 100644 --- a/Sprint-3/alarmclock/style.css +++ b/Sprint-3/alarmclock/style.css @@ -8,4 +8,7 @@ h1 { #alarmSet { margin: 20px; +} +.alarm-activated { + background-color: red; } \ No newline at end of file From a5fab38f0d72f766b6a08511598967c8cdce29d8 Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Fri, 17 Apr 2026 16:02:16 +0100 Subject: [PATCH 4/6] Fix the timer bug --- Sprint-3/alarmclock/alarmclock.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 53fdd8091..839ef3fb3 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -47,17 +47,12 @@ function startTimer() { stopAlarm(); timerId = setInterval(() => { - timeRemaining--; - + timeRemaining = timeRemaining - 1; updateDisplay(timeRemaining); if (timeRemaining <= 0) { clearInterval(timerId); timerId = null; - - timeRemaining = 0; - updateDisplay(timeRemaining); - triggerAlarm(); } }, 1000); From 27648c986cb18c3106094a0dc118c2f57f843673 Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Fri, 17 Apr 2026 16:22:58 +0100 Subject: [PATCH 5/6] fix the timer logic --- Sprint-3/alarmclock/alarmclock.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 839ef3fb3..64420b087 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -24,9 +24,22 @@ function playAlarm() { audio.play(); } +// TRIGGER ALARM +function triggerAlarm() { + document.body.classList.toggle("alarm-activated", true); + playAlarm(); +} + // STOP / RESET ALARM STATE function stopAlarm() { - audio.pause(); + if (typeof audio.pause === "function") { + try { + audio.pause(); + } catch (error) { + // ignore jsdom audio pause errors + } + } + audio.currentTime = 0; audio.loop = false; @@ -36,12 +49,6 @@ function stopAlarm() { document.body.classList.toggle("alarm-activated", false); } -// TRIGGER ALARM -function triggerAlarm() { - document.body.classList.toggle("alarm-activated", true); - playAlarm(); -} - // START TIMER function startTimer() { stopAlarm(); From dc63af62ed17a036f63dcd7dfc0dfcfb58650fbc Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Fri, 17 Apr 2026 19:07:36 +0100 Subject: [PATCH 6/6] Fix the timer behavior and improve the alarm functionality --- Sprint-3/alarmclock/alarmclock.js | 50 ++++++++++++++++++++----------- Sprint-3/alarmclock/index.html | 12 ++++---- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 64420b087..a245429db 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,5 +1,6 @@ let timeRemaining = 0; let timerId = null; +let isPaused = false; const audio = new Audio("alarmsound.mp3"); @@ -14,10 +15,10 @@ function formatTime(seconds) { // UPDATE DISPLAY function updateDisplay(time) { const heading = document.getElementById("timeRemaining"); - heading.textContent = "Time Remaining: " + formatTime(time); + heading.textContent = formatTime(Math.max(0, time)); } -// REQUIRED BY TESTS +// PLAY ALARM function playAlarm() { audio.loop = true; audio.currentTime = 0; @@ -30,28 +31,36 @@ function triggerAlarm() { playAlarm(); } -// STOP / RESET ALARM STATE +// STOP / PAUSE / RESUME function stopAlarm() { - if (typeof audio.pause === "function") { - try { - audio.pause(); - } catch (error) { - // ignore jsdom audio pause errors - } + // 1. If timer is running → pause + if (timerId !== null) { + clearInterval(timerId); + timerId = null; + isPaused = true; + return; } - audio.currentTime = 0; - audio.loop = false; + // 2. If paused → resume + if (isPaused && timeRemaining > 0) { + startTimer(); + isPaused = false; + return; + } - clearInterval(timerId); - timerId = null; + // 3. If alarm is playing → stop it + if (timeRemaining <= 0) { + audio.pause(); + audio.currentTime = 0; + audio.loop = false; - document.body.classList.toggle("alarm-activated", false); + document.body.classList.toggle("alarm-activated", false); + } } // START TIMER function startTimer() { - stopAlarm(); + clearInterval(timerId); timerId = setInterval(() => { timeRemaining = timeRemaining - 1; @@ -60,6 +69,7 @@ function startTimer() { if (timeRemaining <= 0) { clearInterval(timerId); timerId = null; + isPaused = false; triggerAlarm(); } }, 1000); @@ -71,13 +81,17 @@ function setAlarm() { timeRemaining = parseInt(input, 10); if (isNaN(timeRemaining) || timeRemaining <= 0) { - stopAlarm(); - timeRemaining = 0; - updateDisplay(timeRemaining); return; } + // reset alarm state + audio.pause(); + audio.currentTime = 0; + audio.loop = false; + document.body.classList.toggle("alarm-activated", false); + updateDisplay(timeRemaining); + isPaused = false; startTimer(); } diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 6d5fc80f1..852bc05b8 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,19 +1,17 @@ - + Alarm Clock + - -

- Time Remaining: 00:00 -

+

Time Remaining: 00:00

- + - \ No newline at end of file +