diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..99a137788 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,52 @@ -function setAlarm() {} +let timerInterval = null; +let currentTime = 0; + +function setAlarm() { + // Clear any existing timer + if (timerInterval) { + clearInterval(timerInterval); + timerInterval = null; + } + + // Get the input value + const input = document.getElementById("alarmSet"); + let totalSeconds = parseInt(input.value); + + // Validate input (if empty or invalid, default to 0) + if (isNaN(totalSeconds)) { + totalSeconds = 0; + } + + currentTime = totalSeconds; + + // Update the display + updateTimeDisplay(currentTime); + + // Start the countdown + timerInterval = setInterval(() => { + if (currentTime > 0) { + currentTime--; + updateTimeDisplay(currentTime); + + // Check if timer reached zero + if (currentTime === 0) { + clearInterval(timerInterval); + timerInterval = null; + playAlarm(); + // Change background color + document.body.style.backgroundColor = "red"; + } + } + }, 1000); +} + +function updateTimeDisplay(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + const formattedTime = `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`; + const heading = document.getElementById("timeRemaining"); + heading.textContent = `Time Remaining: ${formattedTime}`; +} // DO NOT EDIT BELOW HERE @@ -16,10 +64,25 @@ function setup() { function playAlarm() { audio.play(); + // Play continuously by looping + audio.loop = true; } function pauseAlarm() { audio.pause(); + audio.currentTime = 0; + // Reset background color when alarm is stopped + document.body.style.backgroundColor = ""; + + // Clear any ongoing timer + if (timerInterval) { + clearInterval(timerInterval); + timerInterval = null; + } + + // Reset display to 00:00 + currentTime = 0; + updateTimeDisplay(0); } window.onload = setup; 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
diff --git a/Sprint-3/package.json b/Sprint-3/package.json index 711a5390f..2a009bcd4 100644 --- a/Sprint-3/package.json +++ b/Sprint-3/package.json @@ -26,7 +26,7 @@ "@testing-library/dom": "^10.4.0", "@testing-library/jest-dom": "^6.6.3", "@testing-library/user-event": "^14.6.1", - "jest": "^30.0.4", + "jest": "^30.3.0", "jest-environment-jsdom": "^30.0.4" } }