diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..82864c786 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,42 @@ -function setAlarm() {} +const timeRemaining = document.getElementById("timeRemaining"); +let timer; +let seconds; + +function setAlarm() { + const alarmSetInput = document.getElementById("alarmSet"); + seconds = Number(alarmSetInput.value); + + + if (seconds <= 0 || isNaN(seconds)) { + alert("Please enter a valid number."); + return; + } + + // Stop a previous countdown if there is one + clearInterval(timer); + + // Run immediately + decrementTimer(); + + // decrement each second + timer = setInterval(() => decrementTimer(), 1000); +} + +function decrementTimer() +{ + const minutes = Math.floor(seconds / 60); + const secs = seconds % 60; + + timeRemaining.textContent = `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; + + if (seconds <= 0) { + clearInterval(timer); + playAlarm(); + return; + } + + seconds--; +} // DO NOT EDIT BELOW HERE @@ -23,3 +61,4 @@ function pauseAlarm() { } window.onload = setup; + diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..4a91379d3 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm Clock App