diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..a245429db 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,25 +1,106 @@ -function setAlarm() {} +let timeRemaining = 0; +let timerId = null; +let isPaused = false; -// 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 +function updateDisplay(time) { + const heading = document.getElementById("timeRemaining"); + heading.textContent = formatTime(Math.max(0, time)); } +// PLAY ALARM function playAlarm() { + audio.loop = true; + audio.currentTime = 0; audio.play(); } -function pauseAlarm() { +// TRIGGER ALARM +function triggerAlarm() { + document.body.classList.toggle("alarm-activated", true); + playAlarm(); +} + +// STOP / PAUSE / RESUME +function stopAlarm() { + // 1. If timer is running → pause + if (timerId !== null) { + clearInterval(timerId); + timerId = null; + isPaused = true; + return; + } + + // 2. If paused → resume + if (isPaused && timeRemaining > 0) { + startTimer(); + isPaused = false; + return; + } + + // 3. If alarm is playing → stop it + if (timeRemaining <= 0) { + audio.pause(); + audio.currentTime = 0; + audio.loop = false; + + document.body.classList.toggle("alarm-activated", false); + } +} + +// START TIMER +function startTimer() { + clearInterval(timerId); + + timerId = setInterval(() => { + timeRemaining = timeRemaining - 1; + updateDisplay(timeRemaining); + + if (timeRemaining <= 0) { + clearInterval(timerId); + timerId = null; + isPaused = false; + triggerAlarm(); + } + }, 1000); +} + +// SET ALARM +function setAlarm() { + const input = document.getElementById("alarmSet").value; + timeRemaining = parseInt(input, 10); + + if (isNaN(timeRemaining) || timeRemaining <= 0) { + return; + } + + // reset alarm state audio.pause(); + audio.currentTime = 0; + audio.loop = false; + document.body.classList.toggle("alarm-activated", false); + + updateDisplay(timeRemaining); + isPaused = false; + startTimer(); +} + +// SETUP +function setup() { + document.getElementById("set").addEventListener("click", setAlarm); + document.getElementById("stop").addEventListener("click", stopAlarm); + + updateDisplay(0); } window.onload = setup; diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d9..852bc05b8 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,20 +1,17 @@ - +
- +