diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..2d504fca0 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,6 +1,63 @@ -function setAlarm() {} +let timeRemaining; +let timerInterval = null; -// DO NOT EDIT BELOW HERE +function formatTime(time) { + const minutes = String(Math.floor(time / 60)).padStart(2, "0"); + const seconds = String(time % 60).padStart(2, "0"); + + return `${minutes}:${seconds}`; +} + +function displayAlarm(time) { + const alarmBox = document.getElementById("timeRemaining"); + alarmBox.textContent = `Time Remaining: ${formatTime(time)}`; +} + +function decreaseAlarmTime() { + timeRemaining--; + displayAlarm(timeRemaining); + + if (timeRemaining <= 0) { + clearInterval(timerInterval); + timerInterval = null; + playAlarm(); + return; + } +} + +function resetAlarm() { + clearInterval(timerInterval); + timerInterval = null; + pauseAlarm(); + audio.currentTime = 0; +} + +function setAlarm() { + const setTime = document.getElementById("alarmSet").value; + + const numericTime = parseInt(setTime, 10); + if (isNaN(numericTime)) { + alert( + "Please enter your desired time in numbers, e.g., 120 for 2 minutes." + ); + return; + } + if (numericTime < 0) { + alert("Please enter a non-negative number."); + return; + } + + resetAlarm(); + timeRemaining = numericTime; + + displayAlarm(timeRemaining); + + if (timeRemaining === 0) { + playAlarm(); + } else { + timerInterval = setInterval(decreaseAlarmTime, 1000); + } +} var audio = new Audio("alarmsound.mp3"); diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..66748001e 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,10 +1,10 @@ - + - Title here + Alarm clock app