diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..775e2e5f4 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,56 @@ -function setAlarm() {} +let timer; // let because timer will be assigned a new timeout ID each time. +let timerRunning = false; + +const input = document.getElementById("alarmSet"); + +input.addEventListener("keydown", function (event) { + if (event.key === "-" || event.key === "e") { + event.preventDefault(); + } +}); + +function setAlarm() { + if (timerRunning) { + return; + } + + const input = document.getElementById("alarmSet"); + + // Convert the input value from a string to a number + let seconds = Number(input.value); + + // Don't start the alarm if the input is empty or negative/zero + if (input.value === "" || seconds <= 0) { + return; + } + + timerRunning = true; + document.getElementById("set").disabled = true; + + function countdown() { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + + const formattedMinutes = String(minutes).padStart(2, "0"); + const formattedSeconds = String(remainingSeconds).padStart(2, "0"); + + document.getElementById("timeRemaining").textContent = + `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; + + if (seconds === 0) { + playAlarm(); + input.value = ""; + timerRunning = false; + document.getElementById("set").disabled = false; + return; + } + + seconds--; + timer = setTimeout(countdown, 1000); + } + + countdown(); +} // DO NOT EDIT BELOW HERE diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..0883b6e51 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,16 +1,16 @@ - + - Title here + Alarm clock app

Time Remaining: 00:00

- +