Skip to content

ZA | ITP-May-2025 | Rashaad Ebrahim | Module Data Groups | Alarm Clock #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
72 changes: 71 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,74 @@
function setAlarm() {}
const timeRemainingDisplay = document.querySelector("#timeRemaining");
const alarmSetInput = document.querySelector("#alarmSet");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

querySelector does not work on all nodes within the DOM. It works here, but does not always work, just something to keep in mind.

const bodyContainer = document.querySelector("body");

// Buttons
const stopAlarmBtn = document.querySelector("#stop");
const setAlarmBtn = document.querySelector("#set");

// Disable set button if input is empty
setAlarmBtn.disabled = true;

// Disable stop button if no timer running
stopAlarmBtn.disabled = true;

function init() {
setAlarmBtn.disabled = true;
stopAlarmBtn.disabled = true;
clearInterval(alarmTimer);
audio.currentTime = 0;
bodyContainer.removeAttribute("style");
}

stopAlarmBtn.addEventListener("click", init);

// Only show set button when there is a positive value in the input
function activateSetButton() {
alarmSetInput.addEventListener("input", (e) => {
Number(e.target.value) > 0 && Number.isInteger(Number(e.target.value))
? (setAlarmBtn.disabled = false)
: (setAlarmBtn.disabled = true);
});
}
activateSetButton();

// Global variables
let inputValue, seconds, minutes, hours, alarmTimer;

// Helper functions
function numberStartPadder(num, count = 2, padding = "0") {
return num.toString().padStart(count, padding);
}

function displayAlarmInput(value) {
seconds = numberStartPadder(value % 60);
minutes = numberStartPadder(Math.floor(value / 60));
return (timeRemainingDisplay.innerHTML = `Time Remaining: ${minutes}:${seconds}`);
}

function counter() {
if (inputValue > 0) {
inputValue--;
displayAlarmInput(inputValue);
} else {
clearInterval(alarmTimer);
playAlarm();
bodyContainer.style.backgroundColor = "#FF5252";
stopAlarmBtn.disabled = false;
}
}

// Main function
function setAlarm() {
if (alarmTimer) {
clearInterval(alarmTimer);
}
inputValue = Number(alarmSetInput.value);
alarmSetInput.value = "";
setAlarmBtn.disabled = true;
displayAlarmInput(inputValue);
alarmTimer = setInterval(counter, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
Loading