diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..c41b5675e 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,54 @@ -function setAlarm() {} +function time_convert(num) { + let minutes = Math.floor((num % 3600) / 60); + let seconds = num % 60; + return minutes.toString().padStart(2, '0') + ":" + seconds.toString().padStart(2, '0'); +} +let currentInterval = null; + +function setup() { + document.getElementById("set").addEventListener("click", setAlarm); + document.getElementById("stop").addEventListener("click", pauseAlarm); +} + +function setAlarm() { + if (currentInterval !== null) return; + + const alarmSetInputEl = document.getElementById("alarmSet"); + let timeEl = Number(alarmSetInputEl.value); + if (!Number.isFinite(timeEl) || timeEl <= 0) { + alert("Please enter a valid number greater than 0"); + return; + } + const timeRemainingCounterEl = document.getElementById("timeRemaining"); + const button = document.getElementById("set"); + + button.disabled = true; + + currentInterval = setInterval(() => { + timeEl--; + + timeRemainingCounterEl.textContent = `Time Remaining: ${time_convert(timeEl)}`; + + if (timeEl <= 0) { + clearInterval(currentInterval); + currentInterval = null; + + timeRemainingCounterEl.textContent = "Done!"; + playAlarm(); + button.disabled = false; + } + }, 1000); +} + +function pauseAlarm() { + if (currentInterval !== null) { + clearInterval(currentInterval); + currentInterval = null; + + document.getElementById("set").disabled = false; + } +} + // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..bffbf369c 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm clock app
@@ -18,3 +18,4 @@

Time Remaining: 00:00

+