diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..876b2adc8 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,75 @@ -function setAlarm() {} +let timer; + +function updateDisplay(timeRemaining, heading) { + let minutes = Math.floor(timeRemaining / 60); + let seconds = timeRemaining % 60; + + heading.innerText = + "Time Remaining: " + + String(minutes).padStart(2, "0") + + ":" + + String(seconds).padStart(2, "0"); +} + +function setAlarm() { + let input = document.getElementById("alarmSet"); + let heading = document.getElementById("timeRemaining"); + + let timeRemaining = parseInt(input.value); + + if (isNaN(timeRemaining) || timeRemaining <= 0) { + alert("Please enter a positive number of seconds."); + return; + } + + clearInterval(timer); + pauseAlarm(); + + audio.currentTime = 0; + document.body.style.backgroundColor = ""; + + updateDisplay(timeRemaining, heading); + + function countdown() { + timeRemaining = timeRemaining - 1; + + updateDisplay(timeRemaining, heading); + + if (timeRemaining <= 0) { + clearInterval(timer); + document.body.style.backgroundColor = "red"; + playAlarm(); + } + } + + timer = setInterval(countdown, 1000); +} + +function stopAlarm() { + clearInterval(timer); + pauseAlarm(); +} + +const setButton = document.getElementById("set"); +const stopButton = document.getElementById("stop"); + +setButton.addEventListener( + "click", + function (event) { + event.stopImmediatePropagation(); + setAlarm(); + }, + true +); + +stopButton.addEventListener( + "click", + function (event) { + event.stopImmediatePropagation(); + stopAlarm(); + }, + true +); // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..ff2d3b453 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ - Title here + Alarm clock app