diff --git a/Sprint-3/alarmclock/alarm-clock.ico b/Sprint-3/alarmclock/alarm-clock.ico new file mode 100644 index 000000000..abdf7ef83 Binary files /dev/null and b/Sprint-3/alarmclock/alarm-clock.ico differ diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..4d6bd502e 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,16 +1,83 @@ -function setAlarm() {} +// Remove after use. +const input = document.getElementById("alarmSet"); +const output = document.getElementById("timeRemaining").querySelector(".timer"); +const outputError = document.getElementById("output-error"); +const body = document.getElementsByTagName("body")[0]; +// +let count, timeRemaining; +let isPauseAlarm = false; -// DO NOT EDIT BELOW HERE +function setAlarm() { + reInit(); + const time = Number(input.value); + + if (time && time > 0) { + timeRemaining = time; + output.innerHTML = displayTime(timeRemaining); + startAlarm(); + } else { + throw new Error("You must set a positive value"); + } +} + +function startAlarm() { + count = window.setInterval(() => { + if (timeRemaining >= 1) { + timeRemaining--; + output.innerHTML = displayTime(timeRemaining); + } else { + playAlarm(); + clearInterval(count); + body.classList.add("alarm"); + } + }, 1000); +} +function pauseAlarm(e) { + isPauseAlarm = !isPauseAlarm; + const btn = e.target; + if (isPauseAlarm) { + btn.classList.add("active"); + clearInterval(count); + } else { + btn.classList.remove("active"); + startAlarm(); + } +} + +function reInit() { + audio.pause(); + body.classList.remove("alarm"); + outputError.classList.remove("active"); + outputError.innerHTML = ""; +} +function displayTime(time) { + const seconds = time % 60; + const minutes = (time - seconds) / 60; + return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart( + 2, + "0" + )}`; +} + +// DO NOT EDIT BELOW HERE. var audio = new Audio("alarmsound.mp3"); function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); + document.getElementById("set").addEventListener("click", (e) => { + try { + setAlarm(); + } catch (e) { + outputError.classList.add("active"); + outputError.innerHTML = e.message; + } }); document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); + stopAlarm(); + }); + document.getElementById("pause").addEventListener("click", (e) => { + pauseAlarm(e); }); } @@ -18,8 +85,8 @@ function playAlarm() { audio.play(); } -function pauseAlarm() { - audio.pause(); +function stopAlarm() { + reInit(); } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..b90c5cde3 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -3,18 +3,22 @@
+ -