diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..bb5564431 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,51 @@ -function setAlarm() {} +//this variable holds the time independent of the function +let Timer = null; + +function setAlarm() { + //this pulls the set time from whatever is entered as input on the html page + let inputValue = document.getElementById("alarmSet").value; + + let remainingTime = Number(inputValue); + + if (isNaN(remainingTime) || remainingTime <= 0) { + return; + } + + //Time on screen in MM:SS format + updateTimerDisplay(remainingTime); + + if (Timer !== null) { + clearInterval(Timer); + } + + Timer = setInterval(function () { + remainingTime = remainingTime - 1; //timer is reduced by 1 second + + updateTimerDisplay(remainingTime); //then updated + + // When timer hits zero, stop timer and play alarm + if (remainingTime <= 0) { + clearInterval(Timer); + playAlarm(); + } + }, 1000); +} + + +function updateTimerDisplay(seconds) { + let minutes = Math.floor(seconds / 60); + let secs = seconds % 60; + + // Puts the figures in strings and in a clock-like format + let minuteStr = String(minutes).padStart(2, "0"); + let secondStr = String(secs).padStart(2, "0"); + + // Update the heading text + document.getElementById( + "timeRemaining" + ).innerText = `Time Remaining: ${minuteStr}:${secondStr}`; + +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..9b71b5b6e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -