Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Fathi Kahin | JS2 | Alarmclock, Quote generator, Reading list and Todo-list | Week 3 #174

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
javascript part completed
  • Loading branch information
fhkahin committed Jan 10, 2024
commit 3f44f9ba619b72bd45991610803e4ca1188ce2f7
58 changes: 55 additions & 3 deletions week-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
function setAlarm() {}

// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");
let countdown;

function setAlarm() {
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

let timeInSeconds = parseInt(input.value, 10);

if (isNaN(timeInSeconds) || timeInSeconds <= 0) {
alert("Please enter a valid positive number for the alarm time.");
return;
}

let minutes = Math.floor(timeInSeconds / 60);
let seconds = timeInSeconds % 60;

countdown = setInterval(() => {
seconds--;

if (seconds < 0) {
if (minutes === 0) {
clearInterval(countdown);
playAlarm();
document.body.style.backgroundColor = "red"; // This was just optional, background color will change
// Flash background color (optional)
setTimeout(() => {
document.body.style.backgroundColor = "";
}, 500);
} else {
seconds = 59;
minutes--;
}
}

heading.innerText = `Time Remaining: ${minutes
.toString()
.padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}, 1000);

document.getElementById("stop").addEventListener("click", () => {
clearInterval(countdown);
pauseAlarm();
heading.innerText = "Time Remaining: 00:00";
document.body.style.backgroundColor = ""; // this is to reset background color
});
}

function setup() {
document.getElementById("set").addEventListener("click", () => {
clearInterval(countdown); // Clear the previous countdown interval
setAlarm();
});

document.getElementById("stop").addEventListener("click", () => {
clearInterval(countdown);
pauseAlarm();
document.getElementById("timeRemaining").innerText = "Time Remaining: 00:00";
document.body.style.backgroundColor = ""; // Reset background color
});
}

// Rest of the code remains unchanged

// DO NOT EDIT BELOW HERE
var audio = new Audio("alarmsound.mp3");

function playAlarm() {
audio.play();
}
Expand Down