diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..09fe252a3 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,45 @@ -function setAlarm() {} +let alarmInterval; + +function setAlarm() { + //change the time remaining and set the remaining time + //into the second. + clearInterval(alarmInterval); + + const InputField = document.getElementById("alarmSet"); + let timeRemaining = Number(InputField.value); + + if (!InputField.value || InputField.value < 0) { + alert("Please enter a valid time in seconds"); + return; + } + + function updateDisplay() { + const timeDisplay = document.getElementById("timeRemaining"); + const Minute = Math.floor(timeRemaining / 60); + const Second = timeRemaining % 60; + + const formatMinute = String(Minute).padStart(2, "0"); + const formatSecond = String(Second).padStart(2, "0"); + + timeDisplay.innerText = `Time Remaining: ${formatMinute}:${formatSecond}`; + } + + updateDisplay(); + //set a variable to count three actions: + // count -1; + // Show what is the remaining time; + // if it goes to zero, beep the sound and stop the counting + alarmInterval = setInterval(() => { + timeRemaining = timeRemaining - 1; + + updateDisplay(); + + if (timeRemaining === 0) { + playAlarm(); + clearInterval(alarmInterval); + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -19,7 +60,8 @@ function playAlarm() { } function pauseAlarm() { + clearInterval(alarmInterval); 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..6fdfa258f 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -44,6 +44,7 @@ test("should set heading when button is clicked", () => { button.click(); expect(heading).toHaveTextContent("Time Remaining: 00:19"); + }); test("should split values over 60 seconds into minutes and seconds", () => { diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..048439063 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm Clock
@@ -17,4 +17,4 @@

Time Remaining: 00:00

- + \ No newline at end of file