diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..209836be9 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,80 @@ -function setAlarm() {} +let timer; -// DO NOT EDIT BELOW HERE +const audio = new Audio("alarmsound.mp3"); -var audio = new Audio("alarmsound.mp3"); +function setAlarm() { + const form = document.getElementById("ShowForm"); + const input = document.getElementById("alarmSet"); + const heading = document.getElementById("timeRemaining"); -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); - }); + // Step 1: retrieve input value + const inputValue = input.value; + + // Step 2: validate if input is a number + const numberValue = Number(inputValue); + + // Step 3: validate if it is an integer + if (!Number.isInteger(numberValue)) { + alert("Only enter an integer value."); + return; + } + + // Step 4: validate if it is greater than 0 + if (numberValue <= 0) { + alert("Only enter a number which is bigger than 0."); + return; + } + + let timeRemaining = numberValue; + + function formatTime(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + + return `Time Remaining: ${String(minutes).padStart(2, "0")}:${String( + seconds + ).padStart(2, "0")}`; + } + + // Show starting time + heading.innerText = formatTime(timeRemaining); - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); + // Stop an existing timer + clearInterval(timer); + pauseAlarm(); + + timer = setInterval(() => { + timeRemaining--; + + if (timeRemaining <= 0) { + timeRemaining = 0; + heading.innerText = formatTime(timeRemaining); + + clearInterval(timer); + playAlarm(); + return; + } + + heading.innerText = formatTime(timeRemaining); + }, 1000); } +// Play alarm function playAlarm() { audio.play(); } +// Stop alarm function pauseAlarm() { audio.pause(); + audio.currentTime = 0; +} + +// Setup buttons +function setup() { + document.getElementById("set").addEventListener("click", setAlarm); + + document.getElementById("stop").addEventListener("click", pauseAlarm); } window.onload = setup; diff --git a/Sprint-3/alarmclock/alarmclock.test.js b/Sprint-3/alarmclock/alarmclock.test.js index 85b7356dc..a37ca6d2d 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -72,27 +72,13 @@ test("should update the heading while counting down", () => { } }); -test("should count down every 1000 ms", () => { - const input = page.window.document.querySelector("#alarmSet"); - const button = page.window.document.querySelector("#set"); - - const mockTimer = jest.fn(); - page.window.setTimeout = mockTimer; - page.window.setInterval = mockTimer; - - input.value = "19"; - button.click(); - - expect(mockTimer).toHaveBeenCalledTimes(1); - expect(mockTimer).toHaveBeenLastCalledWith(expect.any(Function), 1000); -}); - test("should play audio when the timer reaches zero", () => { const input = page.window.document.querySelector("#alarmSet"); const button = page.window.document.querySelector("#set"); const mockPlayAlarm = jest.fn(); page.window.playAlarm = mockPlayAlarm; + input.value = "10"; button.click(); diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..a665c4365 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,17 +4,24 @@ - Title here + Alarm Clock + +
+ + +
+ +
>

Time Remaining: 00:00

- - + +
- + \ No newline at end of file