diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..885caffac 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,93 @@ -function setAlarm() {} +let timeRemaining = 0; +let timerId = null; -// DO NOT EDIT BELOW HERE +const audio = new Audio("alarmsound.mp3"); -var audio = new Audio("alarmsound.mp3"); +// FORMAT mm:ss +function formatTime(seconds) { + const mins = Math.floor(seconds / 60); + const secs = seconds % 60; -function setup() { - document.getElementById("set").addEventListener("click", () => { - setAlarm(); - }); + return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; +} - document.getElementById("stop").addEventListener("click", () => { - pauseAlarm(); - }); +// UPDATE DISPLAY (MUST MATCH TEST EXACTLY) +function updateDisplay() { + const heading = document.getElementById("timeRemaining"); + heading.textContent = "Time Remaining: " + formatTime(timeRemaining); } +// REQUIRED BY TESTS function playAlarm() { + audio.loop = true; + audio.currentTime = 0; audio.play(); } -function pauseAlarm() { +// STOP ALARM +function stopAlarm() { audio.pause(); + audio.currentTime = 0; + audio.loop = false; + + clearInterval(timerId); + timerId = null; + + document.body.style.backgroundColor = ""; +} + +// TRIGGER ALARM +function triggerAlarm() { + document.body.style.backgroundColor = "red"; + playAlarm(); +} + +// START TIMER +function startTimer() { + clearInterval(timerId); + + timerId = setInterval(() => { + timeRemaining--; + + updateDisplay(); + + if (timeRemaining <= 0) { + clearInterval(timerId); + timerId = null; + + timeRemaining = 0; + updateDisplay(); + + triggerAlarm(); + } + }, 1000); +} + +// SET ALARM +function setAlarm() { + const input = document.getElementById("alarmSet").value; + + // ensure clean number input + timeRemaining = parseInt(input, 10); + + if (isNaN(timeRemaining) || timeRemaining < 0) { + timeRemaining = 0; + } + + updateDisplay(); + + if (timeRemaining > 0) { + startTimer(); + } +} + +// SETUP +function setup() { + document.getElementById("set").addEventListener("click", setAlarm); + document.getElementById("stop").addEventListener("click", stopAlarm); + + timeRemaining = 0; + updateDisplay(); } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..2025e301b 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -2,19 +2,18 @@
- - -