From b2c81bff54c3609e0a2f8d3a5d08ce4425e8342d Mon Sep 17 00:00:00 2001 From: Hayriye Saricicek Date: Fri, 27 Mar 2026 10:04:37 +0000 Subject: [PATCH 1/5] function for alarm clock --- Sprint-3/alarmclock/alarmclock.js | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..39320415b 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,48 @@ function setAlarm() {} +// Only define Audio if it doesn't exist (Node environment) +if (typeof Audio === "undefined") { + global.Audio = class { + constructor(src) { + this.src = src; + } + play() {} + pause() {} + }; +} + +let intervalId; + +const formatTime = (s) => { + const mins = Math.floor(s / 60); + const secs = s % 60; + return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; +}; + +function setAlarm() { + const input = document.getElementById("alarmSet"); + const timer = document.getElementById("timeRemaining"); + + const seconds = parseInt(input.value); + if (isNaN(seconds) || seconds < 0) return; + + let remaining = seconds; + + // DO NOT EDIT BELOW HERE + timer.textContent = `Time Remaining: ${formatTime(remaining)}`; + + if (intervalId) clearInterval(intervalId); + + intervalId = setInterval(() => { + remaining--; + + timer.textContent = `Time Remaining: ${formatTime(remaining)}`; + + if (remaining <= 0) { + clearInterval(intervalId); + playAlarm(); + } + }, 1000); +} // DO NOT EDIT BELOW HERE From 71f2fa0516162e1555f4fcc2131af46d62c852f8 Mon Sep 17 00:00:00 2001 From: Hayriye Saricicek Date: Wed, 1 Apr 2026 12:59:46 +0100 Subject: [PATCH 2/5] updated variable names and amended code and added comments --- Sprint-3/alarmclock/alarmclock.js | 78 +++++++++++++++---------------- Sprint-3/alarmclock/index.html | 4 +- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 39320415b..1957f9278 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,69 +1,69 @@ -function setAlarm() {} + // Only define Audio if it doesn't exist (Node environment) -if (typeof Audio === "undefined") { - global.Audio = class { - constructor(src) { - this.src = src; +if (typeof Audio === "undefined") { // In a Node define a mock Audio class to prevent errors when calling play() and pause() + global.Audio = class { // Mock Audio class + constructor(src) { // Takes a source for the audio file, but we won't load audio in this mock + this.src = src; // stores the source (unused in the mock) } - play() {} - pause() {} + play() {} // no-op in this mock + pause() {} // no-op in this mock }; } -let intervalId; +let countdownIntervalId; //stores timer ID so we can clear it when needed -const formatTime = (s) => { - const mins = Math.floor(s / 60); - const secs = s % 60; - return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; +const formatTime = (seconds) => { // takes a number of seconds and formats it into a string in the format "MM:SS" + const minutes = Math.floor(seconds / 60); // calculates whole minutes + const remainingSeconds = seconds % 60; // calculates remaining seconds + return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS }; -function setAlarm() { - const input = document.getElementById("alarmSet"); - const timer = document.getElementById("timeRemaining"); - - const seconds = parseInt(input.value); - if (isNaN(seconds) || seconds < 0) return; +function setAlarm() { // called "Set" button is clicked + const alarmInput = document.getElementById("alarmSet"); // gets input for seconds + const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time - let remaining = seconds; + const seconds = parseInt(alarmInput.value, 10); // converts input value to integer + if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input - // DO NOT EDIT BELOW HERE - timer.textContent = `Time Remaining: ${formatTime(remaining)}`; + let remainingSeconds = seconds; // initializes countdown - if (intervalId) clearInterval(intervalId); + timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time - intervalId = setInterval(() => { - remaining--; + if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously - timer.textContent = `Time Remaining: ${formatTime(remaining)}`; + countdownIntervalId = setInterval(() => { // timer runs every second + remainingSeconds--; // decrements remaining time by 1 second - if (remaining <= 0) { - clearInterval(intervalId); - playAlarm(); + if (remainingSeconds <= 0) { // when time runs out, clear the timer and play the alarm sound + clearInterval(countdownIntervalId); // stops the countdown + timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time + playAlarm(); // plays alarm sound + return; // prevents further execution } - }, 1000); + timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display + }, 1000); // repeats every second } // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); +function setup() { // This function sets up event listeners for the "Set" and "Stop" buttons when the page loads + document.getElementById("set").addEventListener("click", () => { // adds a click event listener to the "Set" button that calls the setAlarm function when clicked + setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked }); - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); + document.getElementById("stop").addEventListener("click", () => { // adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked + pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked }); } -function playAlarm() { - audio.play(); +function playAlarm() { // This function plays the alarm sound when called + audio.play(); // calls the play method on the audio object to start playing the alarm sound } -function pauseAlarm() { - audio.pause(); +function pauseAlarm() { // This function pauses the alarm sound when called + audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound } -window.onload = setup; +window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..66748001e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - + - Title here + Alarm clock app
From e7e990dca4fddfe483fb6f3232e9ff038b3ae576 Mon Sep 17 00:00:00 2001 From: Hayriye Saricicek Date: Wed, 1 Apr 2026 14:16:11 +0100 Subject: [PATCH 3/5] saved file --- Sprint-3/alarmclock/alarmclock.js | 79 ++++++++++++++++++------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 1957f9278..2933bf1d1 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,69 +1,80 @@ - // Only define Audio if it doesn't exist (Node environment) -if (typeof Audio === "undefined") { // In a Node define a mock Audio class to prevent errors when calling play() and pause() - global.Audio = class { // Mock Audio class - constructor(src) { // Takes a source for the audio file, but we won't load audio in this mock - this.src = src; // stores the source (unused in the mock) +if (typeof Audio === "undefined") { + // In a Node define a mock Audio class to prevent errors when calling play() and pause() + global.Audio = class { + // Mock Audio class + constructor(src) { + // Takes a source for the audio file, but we won't load audio in this mock + this.src = src; // stores the source (unused in the mock) } - play() {} // no-op in this mock - pause() {} // no-op in this mock + play() {} // no-op in this mock + pause() {} // no-op in this mock }; } -let countdownIntervalId; //stores timer ID so we can clear it when needed +let countdownIntervalId; //stores timer ID so we can clear it when needed -const formatTime = (seconds) => { // takes a number of seconds and formats it into a string in the format "MM:SS" - const minutes = Math.floor(seconds / 60); // calculates whole minutes - const remainingSeconds = seconds % 60; // calculates remaining seconds - return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS +const formatTime = (seconds) => { + // takes a number of seconds and formats it into a string in the format "MM:SS" + const minutes = Math.floor(seconds / 60); // calculates whole minutes + const remainingSeconds = seconds % 60; // calculates remaining seconds + return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS }; -function setAlarm() { // called "Set" button is clicked - const alarmInput = document.getElementById("alarmSet"); // gets input for seconds - const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time +function setAlarm() { + // called "Set" button is clicked + const alarmInput = document.getElementById("alarmSet"); // gets input for seconds + const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time - const seconds = parseInt(alarmInput.value, 10); // converts input value to integer - if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input + const seconds = parseInt(alarmInput.value, 10); // converts input value to integer + if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input let remainingSeconds = seconds; // initializes countdown - timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time + timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously - countdownIntervalId = setInterval(() => { // timer runs every second - remainingSeconds--; // decrements remaining time by 1 second + countdownIntervalId = setInterval(() => { + // timer runs every second + remainingSeconds--; // decrements remaining time by 1 second - if (remainingSeconds <= 0) { // when time runs out, clear the timer and play the alarm sound - clearInterval(countdownIntervalId); // stops the countdown + if (remainingSeconds <= 0) { + // when time runs out, clear the timer and play the alarm sound + clearInterval(countdownIntervalId); // stops the countdown timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time - playAlarm(); // plays alarm sound + playAlarm(); // plays alarm sound return; // prevents further execution } timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display - }, 1000); // repeats every second + }, 1000); // repeats every second } // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); -function setup() { // This function sets up event listeners for the "Set" and "Stop" buttons when the page loads - document.getElementById("set").addEventListener("click", () => { // adds a click event listener to the "Set" button that calls the setAlarm function when clicked - setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked +function setup() { + // This function sets up event listeners for the "Set" and "Stop" buttons when the page loads + document.getElementById("set").addEventListener("click", () => { + // adds a click event listener to the "Set" button that calls the setAlarm function when clicked + setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked }); - document.getElementById("stop").addEventListener("click", () => { // adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked - pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked + document.getElementById("stop").addEventListener("click", () => { + // adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked + pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked }); } -function playAlarm() { // This function plays the alarm sound when called - audio.play(); // calls the play method on the audio object to start playing the alarm sound +function playAlarm() { + // This function plays the alarm sound when called + audio.play(); // calls the play method on the audio object to start playing the alarm sound } -function pauseAlarm() { // This function pauses the alarm sound when called - audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound +function pauseAlarm() { + // This function pauses the alarm sound when called + audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound } -window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly +window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly From 5eecda56f89f64c3ad47df544f47e1ffbb289a8f Mon Sep 17 00:00:00 2001 From: Hayriye Saricicek Date: Thu, 2 Apr 2026 18:15:12 +0100 Subject: [PATCH 4/5] amended so no delay after 0 and have to press stop before set new alarm --- Sprint-3/alarmclock/alarmclock.js | 44 ++++++++++++++++++++++++++++--- Sprint-3/alarmclock/index.html | 2 +- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 2933bf1d1..85d7f31cb 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -13,6 +13,7 @@ if (typeof Audio === "undefined") { } let countdownIntervalId; //stores timer ID so we can clear it when needed +let alarmRunning = false; // tracks if a countdown is currently active const formatTime = (seconds) => { // takes a number of seconds and formats it into a string in the format "MM:SS" @@ -26,22 +27,39 @@ function setAlarm() { const alarmInput = document.getElementById("alarmSet"); // gets input for seconds const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time + // Prevent starting a new alarm if one is already running + if (alarmRunning) { + alert("Please press Stop before setting a new alarm."); // ensues user cannot set another alarm without stopping the current one first + return; + } const seconds = parseInt(alarmInput.value, 10); // converts input value to integer + if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input let remainingSeconds = seconds; // initializes countdown timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time - if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously + if (countdownIntervalId) { + clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously + countdownIntervalId = null; // resets timer ID + } + + if (remainingSeconds === 0) { + // if the input is zero, play the alarm immediately without starting a countdown + playAlarm(); + return; + } + alarmRunning = true; // mark that alarm is active countdownIntervalId = setInterval(() => { - // timer runs every second remainingSeconds--; // decrements remaining time by 1 second if (remainingSeconds <= 0) { // when time runs out, clear the timer and play the alarm sound - clearInterval(countdownIntervalId); // stops the countdown + clearInterval(countdownIntervalId); // stops the countdown timer + countdownIntervalId = null; // resets timer ID + alarmRunning = false; // mark that alarm is no longer active timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time playAlarm(); // plays alarm sound return; // prevents further execution @@ -50,6 +68,26 @@ function setAlarm() { }, 1000); // repeats every second } +window.addEventListener("load", () => { + // Add a listener to the Stop button that also clears the timer + const stopBtn = document.getElementById("stop"); + + stopBtn.addEventListener("click", () => { + // stops countdown amd stops audio + if (countdownIntervalId) { + clearInterval(countdownIntervalId); + countdownIntervalId = null; + } + alarmRunning = false; // mark that alarm is no longer active + + document.getElementById("timeRemaining").textContent = + "Time Remaining: 00:00"; // resets display when stopped + + document.getElementById("alarmSet").value = ""; // clears input field when stopped + pauseAlarm(); // stop audio if it's playing + }); +}); + // DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 66748001e..7aff53a87 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -10,7 +10,7 @@

Time Remaining: 00:00

- + From 6cd5bcf6a16359e38c7f89cee081511f70311a4a Mon Sep 17 00:00:00 2001 From: Hayriye Saricicek Date: Sun, 5 Apr 2026 17:29:52 +0100 Subject: [PATCH 5/5] addedd functions and made amendments --- Sprint-3/alarmclock/alarmclock.js | 40 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 85d7f31cb..edf7690a3 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -14,6 +14,7 @@ if (typeof Audio === "undefined") { let countdownIntervalId; //stores timer ID so we can clear it when needed let alarmRunning = false; // tracks if a countdown is currently active +let alarmRinging = false; // tracks if the alarm sound is currently playing const formatTime = (seconds) => { // takes a number of seconds and formats it into a string in the format "MM:SS" @@ -22,6 +23,21 @@ const formatTime = (seconds) => { return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS }; +function updateTimerDisplay(timerElement, seconds) { + timerElement.textContent = `Time Remaining: ${formatTime(seconds)}`; +} + +function resetAllStates(timerDisplay) { + if (countdownIntervalId) { + clearInterval(countdownIntervalId); + countdownIntervalId = null; + } + alarmRunning = false; + alarmRinging = false; + pauseAlarm(); + updateTimerDisplay(timerDisplay, 0); + document.getElementById("alarmSet").value = ""; +} function setAlarm() { // called "Set" button is clicked const alarmInput = document.getElementById("alarmSet"); // gets input for seconds @@ -32,13 +48,17 @@ function setAlarm() { alert("Please press Stop before setting a new alarm."); // ensues user cannot set another alarm without stopping the current one first return; } + if (alarmRinging) { + resetAllStates(timerDisplay); // stop previous alarm and reset states + } + const seconds = parseInt(alarmInput.value, 10); // converts input value to integer - if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input + if (isNaN(seconds) || seconds < 0) return; // ignore invalid or negative input let remainingSeconds = seconds; // initializes countdown - timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time + updateTimerDisplay(timerDisplay, remainingSeconds); // shows initial time if (countdownIntervalId) { clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously @@ -47,6 +67,7 @@ function setAlarm() { if (remainingSeconds === 0) { // if the input is zero, play the alarm immediately without starting a countdown + alarmRinging = true; // mark that alarm sound is currently playing playAlarm(); return; } @@ -59,12 +80,16 @@ function setAlarm() { // when time runs out, clear the timer and play the alarm sound clearInterval(countdownIntervalId); // stops the countdown timer countdownIntervalId = null; // resets timer ID + alarmRunning = false; // mark that alarm is no longer active - timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time + alarmRinging = true; // mark that alarm sound is currently playing + + updateTimerDisplay(timerDisplay, 0); // shows zero time playAlarm(); // plays alarm sound return; // prevents further execution } - timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display + + updateTimerDisplay(timerDisplay, remainingSeconds); // updates countdown display }, 1000); // repeats every second } @@ -79,12 +104,13 @@ window.addEventListener("load", () => { countdownIntervalId = null; } alarmRunning = false; // mark that alarm is no longer active + alarmRinging = false; // mark that alarm sound is no longer playing + pauseAlarm(); // stop audio if it's playing - document.getElementById("timeRemaining").textContent = - "Time Remaining: 00:00"; // resets display when stopped + const timerDisplay = document.getElementById("timeRemaining"); + updateTimerDisplay(timerDisplay, 0); document.getElementById("alarmSet").value = ""; // clears input field when stopped - pauseAlarm(); // stop audio if it's playing }); });