diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b1..dac10c71b2 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,56 @@ -function setAlarm() {} +let countdownTimer; +let flashTimer; +let currentSeconds; + +function setAlarm() { + clearInterval(countdownTimer); + // fetch seconds input + currentSeconds = Number(document.getElementById("alarmSet").value); + document.body.style.backgroundImage = "url('alarmsethamster.jfif')"; + document.body.style.backgroundPosition = "centre"; + // set time remaining to seconds input + document.getElementById("timeRemaining").textContent = + `Time Remaining: ${formatTime(currentSeconds)}`; + + // handling the interval and alarm end + countdownTimer = setInterval(() => { + currentSeconds = currentSeconds - 1; + + document.getElementById("timeRemaining").textContent = + `Time Remaining: ${formatTime(currentSeconds)}`; + + if (currentSeconds <= 0) { + clearInterval(countdownTimer); + playAlarm(); + document.getElementById("timeRemaining").textContent = + `Time Remaining: 00:00`; + + // img change + document.body.style.backgroundImage = "url('alarmplaysoundhamster.jfif')"; + document.body.style.backgroundPosition = "center"; + + // box turns red + let flashCount = 0; + const box = document.querySelector(".centre"); + flashTimer = setInterval(() => { + box.classList.toggle("red-bg"); + flashCount++ + if (flashCount >= 20){ + clearInterval(flashTimer); + } + }, 500); + } + }, 1000); +} + +function formatTime(totalSeconds) { + // converts the seconds to minutes and seconds and returns "mm:ss" + const convertToMinutes = Math.floor(totalSeconds / 60); + const remainderSeconds = totalSeconds % 60; + const paddedMinutes = String(convertToMinutes).padStart(2, "0"); + const paddedSeconds = String(remainderSeconds).padStart(2, "0"); + return `${paddedMinutes}:${paddedSeconds}`; +} // DO NOT EDIT BELOW HERE @@ -23,3 +75,10 @@ function pauseAlarm() { } window.onload = setup; + +document.getElementById("stop").addEventListener("click", () => { + document.body.style.backgroundImage = "url('alarmsethamster.jfif')"; + document.body.style.backgroundPosition = "center"; + clearInterval(countdownTimer); + clearInterval(flashTimer); +}); \ No newline at end of file diff --git a/Sprint-3/alarmclock/alarmplaysoundhamster.jfif b/Sprint-3/alarmclock/alarmplaysoundhamster.jfif new file mode 100644 index 0000000000..ea0eab44bf Binary files /dev/null and b/Sprint-3/alarmclock/alarmplaysoundhamster.jfif differ diff --git a/Sprint-3/alarmclock/alarmplaysoundhamster.jfif:Zone.Identifier b/Sprint-3/alarmclock/alarmplaysoundhamster.jfif:Zone.Identifier new file mode 100644 index 0000000000..d6c1ec6829 Binary files /dev/null and b/Sprint-3/alarmclock/alarmplaysoundhamster.jfif:Zone.Identifier differ diff --git a/Sprint-3/alarmclock/alarmsethamster.jfif b/Sprint-3/alarmclock/alarmsethamster.jfif new file mode 100644 index 0000000000..ac82471778 Binary files /dev/null and b/Sprint-3/alarmclock/alarmsethamster.jfif differ diff --git a/Sprint-3/alarmclock/alarmsethamster.jfif:Zone.Identifier b/Sprint-3/alarmclock/alarmsethamster.jfif:Zone.Identifier new file mode 100644 index 0000000000..d6c1ec6829 Binary files /dev/null and b/Sprint-3/alarmclock/alarmsethamster.jfif:Zone.Identifier differ diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d99..ff2d3b4532 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -4,7 +4,7 @@ -