From f622d48fb6cc444569447f1f75379936a749aad7 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 21 Jul 2025 19:34:51 +0100 Subject: [PATCH 1/9] Sprint3-alarmclock --- Sprint-3/alarmclock/alarmclock.js | 56 +++++++++++++++++++++++++- Sprint-3/alarmclock/alarmclock.test.js | 2 +- Sprint-3/alarmclock/alarmclockapp.html | 20 +++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 Sprint-3/alarmclock/alarmclockapp.html diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..15b55ac11 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,54 @@ -function setAlarm() {} +timeSetup = document.querySelector("#alarmSet"); // Access to alarmset + +timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemaining + +let countdownInterval; // Variable to setup to 0 the countdown + +function setAlarm() { + const totalSeconds = parseInt(timeSetup.value, 10); + const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); + const seconds = String(totalSeconds % 60).padStart(2, "0"); + + timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; + + document.body.style.backgroundColor = ""; // Reset background color + +} + + +function startCountdown() { + + let totalSeconds = parseInt(timeSetup.value, 10); //Convert the the string seconds to a number + + if (isNaN(totalSeconds) || totalSeconds <= 0) { + timeCountdown.innerText = "Please enter a valid time."; // Return if the input is not a number or less than or equal to 0 + return; + } + + clearInterval(countdownInterval); // Stop previous countdown if any + + countdownInterval = setInterval(() => { + const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); + const seconds = String(totalSeconds % 60).padStart(2, "0"); + + timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; + + if (totalSeconds <= 0) { + clearInterval(countdownInterval); + timeCountdown.innerText = "Time's up!"; + + document.body.style.backgroundColor = "#ff4c4c"; // Change background color Bright red + + playAlarm(); // Play the alarm sound when the countdown reaches zero + } + + totalSeconds--; + }, 1000); + +} + + + // DO NOT EDIT BELOW HERE @@ -6,7 +56,8 @@ var audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { - setAlarm(); + setAlarm(); // Sets the initial time display + startCountdown(); // Starts the actual countdown }); document.getElementById("stop").addEventListener("click", () => { @@ -23,3 +74,4 @@ function pauseAlarm() { } window.onload = setup; + diff --git a/Sprint-3/alarmclock/alarmclock.test.js b/Sprint-3/alarmclock/alarmclock.test.js index 85b7356dc..9abeb618b 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -8,7 +8,7 @@ const { JSDOM } = require("jsdom"); let page = null; beforeEach(async () => { - page = await JSDOM.fromFile(path.join(__dirname, "index.html"), { + page = await JSDOM.fromFile(path.join(__dirname, "alarmclockapp.html"), { resources: "usable", runScripts: "dangerously", }); diff --git a/Sprint-3/alarmclock/alarmclockapp.html b/Sprint-3/alarmclock/alarmclockapp.html new file mode 100644 index 000000000..6e2a873fa --- /dev/null +++ b/Sprint-3/alarmclock/alarmclockapp.html @@ -0,0 +1,20 @@ + + + + + + + Title here + + +
+

Time Remaining: 00:00

+ + + + + +
+ + + From d23f10ab592619958a04bf86a6686875e50fd1ac Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 21 Jul 2025 19:41:39 +0100 Subject: [PATCH 2/9] Sprint3-AlarmClock --- Sprint-3/alarmclock/alarmclock.js | 4 ++-- Sprint-3/alarmclock/alarmclock.test.js | 2 +- Sprint-3/alarmclock/alarmclockapp.html | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 15b55ac11..f4cabe66e 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -66,11 +66,11 @@ function setup() { } function playAlarm() { - audio.play(); + audio.play(); // Play the alarm sound } function pauseAlarm() { - audio.pause(); + audio.pause(); // Stop the alarm sound } window.onload = setup; diff --git a/Sprint-3/alarmclock/alarmclock.test.js b/Sprint-3/alarmclock/alarmclock.test.js index 9abeb618b..426097dd3 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -1,5 +1,5 @@ /* ======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. +There are some Tests in this file that will help you work out if your code is working. No need to change the code in this file. */ const path = require("path"); diff --git a/Sprint-3/alarmclock/alarmclockapp.html b/Sprint-3/alarmclock/alarmclockapp.html index 6e2a873fa..4efd4daee 100644 --- a/Sprint-3/alarmclock/alarmclockapp.html +++ b/Sprint-3/alarmclock/alarmclockapp.html @@ -9,7 +9,7 @@

Time Remaining: 00:00

- + From 712efa4b01045249ea5871db3f5380f37a567754 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Tue, 22 Jul 2025 20:04:20 +0100 Subject: [PATCH 3/9] Sprint3/quote-generator --- Sprint-3/quote-generator/index.html | 48 +++++++++++++++++++++++++++-- Sprint-3/quote-generator/quotes.js | 32 +++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/Sprint-3/quote-generator/index.html b/Sprint-3/quote-generator/index.html index 30b434bcf..9f9dffc7e 100644 --- a/Sprint-3/quote-generator/index.html +++ b/Sprint-3/quote-generator/index.html @@ -3,13 +3,55 @@ - Title here + Quote generator app -

hello there

- +
+ + + + + diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index 4a4d04b72..cc011d250 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -491,3 +491,35 @@ const quotes = [ ]; // call pickFromArray with the quotes array to check you get a random quote + +let autoGenerate = false; +let autoGenerateInterval = null; +buttonAuto = document.querySelector("#quote-generator"); + +function displayQuote() { + const randomQuote = pickFromArray(quotes); + document.getElementById("quote").textContent = `"${randomQuote.quote}"`; + document.getElementById("author").textContent = `— ${randomQuote.author}`; +} + +// Show one on page load +displayQuote(); + +function autoGenerateQuotes() { + if (autoGenerate) { + clearInterval(autoGenerateInterval); + autoGenerate = false; + buttonAuto.textContent = "Start Auto-Generate"; + } + else { + autoGenerate = true; + buttonAuto.textContent = "Stop Auto-Generate"; + autoGenerateInterval = setInterval(displayQuote, 5000); // Change quote every 5 seconds + } +} + +// Add event listener to the button new quote +document.getElementById("new-quote").addEventListener("click", displayQuote); + +// Add event listener to the button quote-generator +document.getElementById("quote-generator").addEventListener("click", autoGenerateQuotes); From 92881f4a66e415f9999d6310295c92013e783b49 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Sun, 27 Jul 2025 18:16:40 +0100 Subject: [PATCH 4/9] Sprint3-alarmaclock-review --- Sprint-3/alarmclock/alarmclock.js | 60 ++++++++++++++++++------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index f4cabe66e..a5fb050b6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -4,15 +4,23 @@ timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemai let countdownInterval; // Variable to setup to 0 the countdown + function setAlarm() { - const totalSeconds = parseInt(timeSetup.value, 10); + const rawInput = timeSetup.value.trim(); // Remove surrounding spaces + const totalSeconds = parseInt(rawInput, 10); + + // Validate the input + if (isNaN(totalSeconds) || totalSeconds < 0) { + timeCountdown.innerText = "Please enter a valid number of seconds."; + document.body.style.backgroundColor = ""; + return; + } + const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); const seconds = String(totalSeconds % 60).padStart(2, "0"); - - timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; - - document.body.style.backgroundColor = ""; // Reset background color + timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; + document.body.style.backgroundColor = ""; // Reset background } @@ -21,33 +29,35 @@ function startCountdown() { let totalSeconds = parseInt(timeSetup.value, 10); //Convert the the string seconds to a number if (isNaN(totalSeconds) || totalSeconds <= 0) { - timeCountdown.innerText = "Please enter a valid time."; // Return if the input is not a number or less than or equal to 0 + timeCountdown.innerText = "Time Remaining: 00:00"; // Return if the input is not a number or less than or equal to 0 return; } clearInterval(countdownInterval); // Stop previous countdown if any - countdownInterval = setInterval(() => { - const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); - const seconds = String(totalSeconds % 60).padStart(2, "0"); - - timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; - - if (totalSeconds <= 0) { - clearInterval(countdownInterval); - timeCountdown.innerText = "Time's up!"; - - document.body.style.backgroundColor = "#ff4c4c"; // Change background color Bright red - - playAlarm(); // Play the alarm sound when the countdown reaches zero - } - - totalSeconds--; - }, 1000); - -} + // Display the first time immediately + const updateDisplay = () => { + const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); + const seconds = String(totalSeconds % 60).padStart(2, "0"); + timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; +}; + updateDisplay(); // Show first tick instantly + countdownInterval = setInterval(() => { + totalSeconds--; + + if (totalSeconds < 0) { + clearInterval(countdownInterval); + timeCountdown.innerText = "Time's up!"; + document.body.style.backgroundColor = "#ff4c4c"; + playAlarm(); + return; + } + + updateDisplay(); + }, 1000); + } // DO NOT EDIT BELOW HERE From b41e30b09becbfd158b26d5a109ba4390d0e47b9 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 28 Jul 2025 15:04:20 +0100 Subject: [PATCH 5/9] Sprint-3/alamarclock-review --- Sprint-3/alarmclock/alarmclock.js | 83 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index a5fb050b6..43c7db009 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,63 +1,58 @@ -timeSetup = document.querySelector("#alarmSet"); // Access to alarmset - -timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemaining - -let countdownInterval; // Variable to setup to 0 the countdown +let timeSetup = document.querySelector("#alarmSet"); +let timeCountdown = document.querySelector("#timeRemaining"); +let countdownInterval; + +// Shared display update function +function updateDisplay(seconds) { + const minutes = String(Math.floor(seconds / 60)).padStart(2, "0"); + const secs = String(seconds % 60).padStart(2, "0"); + timeCountdown.innerText = `Time Remaining: ${minutes}:${secs}`; +} +// Shared validation function +function isValidInput(value) { + const num = parseInt(value.trim(), 10); + return !isNaN(num) && num > 0 ? num : null; +} function setAlarm() { - const rawInput = timeSetup.value.trim(); // Remove surrounding spaces - const totalSeconds = parseInt(rawInput, 10); + const totalSeconds = isValidInput(timeSetup.value); - // Validate the input - if (isNaN(totalSeconds) || totalSeconds < 0) { + if (totalSeconds === null) { timeCountdown.innerText = "Please enter a valid number of seconds."; document.body.style.backgroundColor = ""; return; } - const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); - const seconds = String(totalSeconds % 60).padStart(2, "0"); - - timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; - document.body.style.backgroundColor = ""; // Reset background + updateDisplay(totalSeconds); + document.body.style.backgroundColor = ""; } - function startCountdown() { + let totalSeconds = isValidInput(timeSetup.value); - let totalSeconds = parseInt(timeSetup.value, 10); //Convert the the string seconds to a number - - if (isNaN(totalSeconds) || totalSeconds <= 0) { - timeCountdown.innerText = "Time Remaining: 00:00"; // Return if the input is not a number or less than or equal to 0 + if (totalSeconds === null) { + timeCountdown.innerText = "Time Remaining: 00:00"; return; } - clearInterval(countdownInterval); // Stop previous countdown if any - - // Display the first time immediately - const updateDisplay = () => { - const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); - const seconds = String(totalSeconds % 60).padStart(2, "0"); - timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; -}; - - updateDisplay(); // Show first tick instantly - - countdownInterval = setInterval(() => { - totalSeconds--; - - if (totalSeconds < 0) { - clearInterval(countdownInterval); - timeCountdown.innerText = "Time's up!"; - document.body.style.backgroundColor = "#ff4c4c"; - playAlarm(); - return; - } - - updateDisplay(); - }, 1000); - } + clearInterval(countdownInterval); // Stop previous countdown + updateDisplay(totalSeconds); // Show first tick immediately + + countdownInterval = setInterval(() => { + totalSeconds--; + + if (totalSeconds < 0) { + clearInterval(countdownInterval); + timeCountdown.innerText = "Time's up!"; + document.body.style.backgroundColor = "#ff4c4c"; + playAlarm(); + return; + } + + updateDisplay(totalSeconds); + }, 1000); +} // DO NOT EDIT BELOW HERE From 7cc8268cd89f975ca922c89d552a1ab4e61937f8 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 28 Jul 2025 20:21:29 +0100 Subject: [PATCH 6/9] Sprint3-alamarclock/Review --- Sprint-3/alarmclock/alarmclock.js | 35 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 43c7db009..0e90d920a 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,5 +1,5 @@ -let timeSetup = document.querySelector("#alarmSet"); -let timeCountdown = document.querySelector("#timeRemaining"); +const timeSetup = document.querySelector("#alarmSet"); +const timeCountdown = document.querySelector("#timeRemaining"); let countdownInterval; // Shared display update function @@ -9,32 +9,35 @@ function updateDisplay(seconds) { timeCountdown.innerText = `Time Remaining: ${minutes}:${secs}`; } -// Shared validation function -function isValidInput(value) { - const num = parseInt(value.trim(), 10); - return !isNaN(num) && num > 0 ? num : null; + +function parsePositiveInteger(value) { + if (typeof value !== "string") return null; + const trimmed = value.trim(); + + // Check if it's all digits (no letters or symbols) + if (!/^\d+$/.test(trimmed)) return null; + + const num = Number(trimmed); + return num > 0 ? num : null; } function setAlarm() { - const totalSeconds = isValidInput(timeSetup.value); + const totalSeconds = parsePositiveInteger(timeSetup.value); if (totalSeconds === null) { timeCountdown.innerText = "Please enter a valid number of seconds."; document.body.style.backgroundColor = ""; return; } - + + startCountdown(totalSeconds); // Start the countdown timer updateDisplay(totalSeconds); document.body.style.backgroundColor = ""; -} + -function startCountdown() { - let totalSeconds = isValidInput(timeSetup.value); +} - if (totalSeconds === null) { - timeCountdown.innerText = "Time Remaining: 00:00"; - return; - } +function startCountdown(totalSeconds) { clearInterval(countdownInterval); // Stop previous countdown updateDisplay(totalSeconds); // Show first tick immediately @@ -62,7 +65,7 @@ var audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { setAlarm(); // Sets the initial time display - startCountdown(); // Starts the actual countdown + //startCountdown(); // Starts the actual countdown }); document.getElementById("stop").addEventListener("click", () => { From f641ff3e082f8945dec0674301941d6f5a48506e Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Mon, 28 Jul 2025 20:40:49 +0100 Subject: [PATCH 7/9] Quote-generator-remove --- Sprint-3/quote-generator/index.html | 48 ++--------------------------- Sprint-3/quote-generator/quotes.js | 32 ------------------- 2 files changed, 3 insertions(+), 77 deletions(-) diff --git a/Sprint-3/quote-generator/index.html b/Sprint-3/quote-generator/index.html index 9f9dffc7e..30b434bcf 100644 --- a/Sprint-3/quote-generator/index.html +++ b/Sprint-3/quote-generator/index.html @@ -3,55 +3,13 @@ - Quote generator app + Title here +

hello there

-
- - + - - - diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index cc011d250..4a4d04b72 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -491,35 +491,3 @@ const quotes = [ ]; // call pickFromArray with the quotes array to check you get a random quote - -let autoGenerate = false; -let autoGenerateInterval = null; -buttonAuto = document.querySelector("#quote-generator"); - -function displayQuote() { - const randomQuote = pickFromArray(quotes); - document.getElementById("quote").textContent = `"${randomQuote.quote}"`; - document.getElementById("author").textContent = `— ${randomQuote.author}`; -} - -// Show one on page load -displayQuote(); - -function autoGenerateQuotes() { - if (autoGenerate) { - clearInterval(autoGenerateInterval); - autoGenerate = false; - buttonAuto.textContent = "Start Auto-Generate"; - } - else { - autoGenerate = true; - buttonAuto.textContent = "Stop Auto-Generate"; - autoGenerateInterval = setInterval(displayQuote, 5000); // Change quote every 5 seconds - } -} - -// Add event listener to the button new quote -document.getElementById("new-quote").addEventListener("click", displayQuote); - -// Add event listener to the button quote-generator -document.getElementById("quote-generator").addEventListener("click", autoGenerateQuotes); From 49aeb27a85b9a4e4fba52a0ab4fe8a9e6d87ab25 Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Wed, 30 Jul 2025 22:08:23 +0100 Subject: [PATCH 8/9] Sprint3-Alarmclock-Review --- Sprint-3/alarmclock/alarmclock.js | 66 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 0e90d920a..102c9061b 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,51 +1,49 @@ -const timeSetup = document.querySelector("#alarmSet"); -const timeCountdown = document.querySelector("#timeRemaining"); -let countdownInterval; - -// Shared display update function -function updateDisplay(seconds) { - const minutes = String(Math.floor(seconds / 60)).padStart(2, "0"); - const secs = String(seconds % 60).padStart(2, "0"); - timeCountdown.innerText = `Time Remaining: ${minutes}:${secs}`; -} - +timeSetup = document.querySelector("#alarmSet"); // Access to alarmset -function parsePositiveInteger(value) { - if (typeof value !== "string") return null; - const trimmed = value.trim(); +timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemaining - // Check if it's all digits (no letters or symbols) - if (!/^\d+$/.test(trimmed)) return null; +let countdownInterval; // Variable to setup to 0 the countdown - const num = Number(trimmed); - return num > 0 ? num : null; -} function setAlarm() { - const totalSeconds = parsePositiveInteger(timeSetup.value); + const rawInput = timeSetup.value.trim(); // Remove surrounding spaces + const totalSeconds = parseInt(rawInput, 10); - if (totalSeconds === null) { + // Validate the input + if (isNaN(totalSeconds) || totalSeconds < 0) { timeCountdown.innerText = "Please enter a valid number of seconds."; document.body.style.backgroundColor = ""; return; } - - startCountdown(totalSeconds); // Start the countdown timer - updateDisplay(totalSeconds); - document.body.style.backgroundColor = ""; - + const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); + const seconds = String(totalSeconds % 60).padStart(2, "0"); + + timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; + document.body.style.backgroundColor = ""; // Reset background } -function startCountdown(totalSeconds) { + +function startCountdown() { + let totalSeconds = parseInt(timeSetup.value, 10); + + if (isNaN(totalSeconds) || totalSeconds <= 0) { + timeCountdown.innerText = "Time Remaining: 00:00"; + return; + } clearInterval(countdownInterval); // Stop previous countdown - updateDisplay(totalSeconds); // Show first tick immediately - countdownInterval = setInterval(() => { - totalSeconds--; + const updateDisplay = () => { + const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); + const seconds = String(totalSeconds % 60).padStart(2, "0"); + timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; + }; - if (totalSeconds < 0) { + updateDisplay(); // Show first tick immediately + + countdownInterval = setInterval(() => { + if (totalSeconds <= 0) { clearInterval(countdownInterval); timeCountdown.innerText = "Time's up!"; document.body.style.backgroundColor = "#ff4c4c"; @@ -53,11 +51,13 @@ function startCountdown(totalSeconds) { return; } - updateDisplay(totalSeconds); + totalSeconds--; // Decrement only after the check + updateDisplay(); }, 1000); } + // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); @@ -65,7 +65,7 @@ var audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { setAlarm(); // Sets the initial time display - //startCountdown(); // Starts the actual countdown + startCountdown(); // Starts the actual countdown }); document.getElementById("stop").addEventListener("click", () => { From 3886e8c6f9fe47bebee453f98c42397148685d2b Mon Sep 17 00:00:00 2001 From: delmorallopez <124817272+delmorallopez@users.noreply.github.com> Date: Thu, 31 Jul 2025 18:23:41 +0100 Subject: [PATCH 9/9] Sprint3-Alarmclock/Review --- Sprint-3/alarmclock/alarmclock.js | 68 +++++++++++++++---------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 102c9061b..e62983c45 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,48 +1,47 @@ -timeSetup = document.querySelector("#alarmSet"); // Access to alarmset +const timeSetup = document.querySelector("#alarmSet"); +const timeCountdown = document.querySelector("#timeRemaining"); +let countdownInterval; + +// Shared display update function +function updateDisplay(seconds) { + const minutes = String(Math.floor(seconds / 60)).padStart(2, "0"); + const secs = String(seconds % 60).padStart(2, "0"); + timeCountdown.innerText = `Time Remaining: ${minutes}:${secs}`; +} + -timeCountdown = document.querySelector("#timeRemaining"); // Access to timeRemaining +function parsePositiveInteger(value) { + if (typeof value !== "string") return null; + const trimmed = value.trim(); -let countdownInterval; // Variable to setup to 0 the countdown + // Check if it's all digits (no letters or symbols) + if (!/^\d+$/.test(trimmed)) return null; + const num = Number(trimmed); + return num > 0 ? num : null; +} function setAlarm() { - const rawInput = timeSetup.value.trim(); // Remove surrounding spaces - const totalSeconds = parseInt(rawInput, 10); + const totalSeconds = parsePositiveInteger(timeSetup.value); - // Validate the input - if (isNaN(totalSeconds) || totalSeconds < 0) { + if (totalSeconds === null) { timeCountdown.innerText = "Please enter a valid number of seconds."; document.body.style.backgroundColor = ""; return; } - - const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); - const seconds = String(totalSeconds % 60).padStart(2, "0"); - - timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; - document.body.style.backgroundColor = ""; // Reset background + + startCountdown(totalSeconds); // Start the countdown timer + document.body.style.backgroundColor = ""; + } - -function startCountdown() { - let totalSeconds = parseInt(timeSetup.value, 10); - - if (isNaN(totalSeconds) || totalSeconds <= 0) { - timeCountdown.innerText = "Time Remaining: 00:00"; - return; - } - +function startCountdown(totalSeconds) { clearInterval(countdownInterval); // Stop previous countdown - - const updateDisplay = () => { - const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); - const seconds = String(totalSeconds % 60).padStart(2, "0"); - timeCountdown.innerText = `Time Remaining: ${minutes}:${seconds}`; - }; - - updateDisplay(); // Show first tick immediately + updateDisplay(totalSeconds); // Show first tick immediately countdownInterval = setInterval(() => { + totalSeconds--; + if (totalSeconds <= 0) { clearInterval(countdownInterval); timeCountdown.innerText = "Time's up!"; @@ -51,13 +50,11 @@ function startCountdown() { return; } - totalSeconds--; // Decrement only after the check - updateDisplay(); + updateDisplay(totalSeconds); }, 1000); } - // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); @@ -65,7 +62,7 @@ var audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { setAlarm(); // Sets the initial time display - startCountdown(); // Starts the actual countdown + //startCountdown(); // Starts the actual countdown }); document.getElementById("stop").addEventListener("click", () => { @@ -81,5 +78,4 @@ function pauseAlarm() { audio.pause(); // Stop the alarm sound } -window.onload = setup; - +window.onload = setup; \ No newline at end of file