diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..7962be1c6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,7 +1,64 @@ -function setAlarm() {} +let timer = null; +let secondsRemaining = 0; -// 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); + timer = null; + } + + 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(() => { + secondsRemaining--; + updateDisplay(secondsRemaining); + + 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"); function setup() { @@ -22,4 +79,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 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() { 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