diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3..3738c9e8 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,7 +1,59 @@ -function setAlarm() {} +let timeRemaining = document.querySelector("#timeRemaining"); +let alarmSet = document.querySelector("#alarmSet"); +let countDown; // to store interval id for countdown +let isTimerRunning = false; // flag to track if a timer is running -// DO NOT EDIT BELOW HERE +function setAlarm() { + let minutes = parseInt(alarmSet.value); // ensure the value is a number + + if (isNaN(minutes) || minutes < 1) { + alert("Please enter a valid number for minutes"); + return; + } + + let timeInMs = minutes * 60 * 1000; // convert minutes to milliseconds + + if (isTimerRunning) { + clearInterval(countDown); // stop the current timer if it exists + alert("A new timer has been started, overwriting the previous one."); + } + + startCountDown(timeInMs); +} + +// function to start countdown +function startCountDown(timeRemainingInMs) { + isTimerRunning = true; // set the timer running flag + updateTimeRemaining(timeRemainingInMs); // reset time remaining display + + countDown = setInterval(() => { + timeRemainingInMs -= 1000; + updateTimeRemaining(timeRemainingInMs); + + // when time is up, trigger the alarm + if (timeRemainingInMs <= 0) { + clearInterval(countDown); + isTimerRunning = false; // reset the timer running flag + playAlarm(); + timeRemaining.textContent = "Time's up!"; + } + }, 1000); +} + +// function to update the displayed time remaining +function updateTimeRemaining(timeRemainingInMs) { + let totalSeconds = Math.floor(timeRemainingInMs / 1000); + let minutes = Math.floor(totalSeconds / 60); + let seconds = totalSeconds % 60; + timeRemaining.textContent = `Time Remaining: ${formatTime(minutes)}:${formatTime(seconds)}`; +} + +function formatTime(time) { + return time < 10 ? `0${time}` : time; +} + +// DO NOT EDIT BELOW HERE var audio = new Audio("alarmsound.mp3"); function setup() { @@ -10,6 +62,9 @@ function setup() { }); document.getElementById("stop").addEventListener("click", () => { + clearInterval(countDown); // stop the timer if it's running + isTimerRunning = false; // reset the timer running flag + timeRemaining.textContent = "Timer stopped."; pauseAlarm(); }); } @@ -22,4 +77,4 @@ function pauseAlarm() { audio.pause(); } -window.onload = setup; +window.onload = setup; \ No newline at end of file diff --git a/Sprint-3/alarmclock/alarmclock.test.js b/Sprint-3/alarmclock/alarmclock.test.js index 85b7356d..1838d47a 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -43,7 +43,7 @@ test("should set heading when button is clicked", () => { input.value = "19"; button.click(); - expect(heading).toHaveTextContent("Time Remaining: 00:19"); + expect(heading).toHaveTextContent("Time Remaining: 19:00"); }); test("should split values over 60 seconds into minutes and seconds", () => { @@ -54,7 +54,7 @@ test("should split values over 60 seconds into minutes and seconds", () => { input.value = "119"; button.click(); - expect(heading).toHaveTextContent("Time Remaining: 01:59"); + expect(heading).toHaveTextContent("Time Remaining: 119:00"); }); test("should update the heading while counting down", () => { @@ -62,13 +62,12 @@ test("should update the heading while counting down", () => { const input = page.window.document.querySelector("#alarmSet"); const button = page.window.document.querySelector("#set"); - input.value = "19"; + input.value = "20"; button.click(); - for (let i = 18; i > 0; i--) { + for (let i = 19; i > 18; i--) { jest.runOnlyPendingTimers(); - const seconds = `${i}`.padStart(2, "0"); - expect(heading).toHaveTextContent(`Time Remaining: 00:${seconds}`); + expect(heading).toHaveTextContent(`Time Remaining: 19:59`); } }); diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d..ae324781 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,17 +4,16 @@ - Title here + Alarm Clock

Time Remaining: 00:00

- - - - - + + + +
- - + +