diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..e62983c45 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,59 @@ -function setAlarm() {} +const timeSetup = document.querySelector("#alarmSet"); +const timeCountdown = document.querySelector("#timeRemaining"); +let countdownInterval; + +// Shared display update function +function updateDisplay(seconds) { + const minutes = String(Math.floor(seconds / 60)).padStart(2, "0"); + const secs = String(seconds % 60).padStart(2, "0"); + timeCountdown.innerText = `Time Remaining: ${minutes}:${secs}`; +} + + +function parsePositiveInteger(value) { + if (typeof value !== "string") return null; + const trimmed = value.trim(); + + // Check if it's all digits (no letters or symbols) + if (!/^\d+$/.test(trimmed)) return null; + + const num = Number(trimmed); + return num > 0 ? num : null; +} + +function setAlarm() { + const totalSeconds = parsePositiveInteger(timeSetup.value); + + if (totalSeconds === null) { + timeCountdown.innerText = "Please enter a valid number of seconds."; + document.body.style.backgroundColor = ""; + return; + } + + startCountdown(totalSeconds); // Start the countdown timer + document.body.style.backgroundColor = ""; + +} + +function startCountdown(totalSeconds) { + clearInterval(countdownInterval); // Stop previous countdown + updateDisplay(totalSeconds); // Show first tick immediately + + countdownInterval = setInterval(() => { + totalSeconds--; + + if (totalSeconds <= 0) { + clearInterval(countdownInterval); + timeCountdown.innerText = "Time's up!"; + document.body.style.backgroundColor = "#ff4c4c"; + playAlarm(); + return; + } + + updateDisplay(totalSeconds); + }, 1000); +} + // DO NOT EDIT BELOW HERE @@ -6,7 +61,8 @@ var audio = new Audio("alarmsound.mp3"); function setup() { document.getElementById("set").addEventListener("click", () => { - setAlarm(); + setAlarm(); // Sets the initial time display + //startCountdown(); // Starts the actual countdown }); document.getElementById("stop").addEventListener("click", () => { @@ -15,11 +71,11 @@ function setup() { } function playAlarm() { - audio.play(); + audio.play(); // Play the alarm sound } function pauseAlarm() { - audio.pause(); + audio.pause(); // Stop the alarm sound } -window.onload = setup; +window.onload = setup; \ No newline at end of file diff --git a/Sprint-3/alarmclock/alarmclock.test.js b/Sprint-3/alarmclock/alarmclock.test.js index 85b7356dc..426097dd3 100644 --- a/Sprint-3/alarmclock/alarmclock.test.js +++ b/Sprint-3/alarmclock/alarmclock.test.js @@ -1,5 +1,5 @@ /* ======= TESTS - DO NOT MODIFY ===== -There are some Tests in this file that will help you work out if your code is working. +There are some Tests in this file that will help you work out if your code is working. No need to change the code in this file. */ const path = require("path"); @@ -8,7 +8,7 @@ const { JSDOM } = require("jsdom"); let page = null; beforeEach(async () => { - page = await JSDOM.fromFile(path.join(__dirname, "index.html"), { + page = await JSDOM.fromFile(path.join(__dirname, "alarmclockapp.html"), { resources: "usable", runScripts: "dangerously", }); diff --git a/Sprint-3/alarmclock/alarmclockapp.html b/Sprint-3/alarmclock/alarmclockapp.html new file mode 100644 index 000000000..4efd4daee --- /dev/null +++ b/Sprint-3/alarmclock/alarmclockapp.html @@ -0,0 +1,20 @@ + + +
+ + + +