I would appreciate any help. I want to change the text color of the button every time it is a darker background color. I have been trying other variations of the below code. I cant seem to get the newColor to work. Thanks in advance for your help.
const button = document.querySelector('button');
const h1 = document.querySelector('h1');
button.addEventListener('click', () => {
const newColor = randomColor();
document.body.style.backgroundColor = newColor;
h1.innerText = newColor;
})
let newColor;
const randomColor = () => {
const r = Math.floor(Math.random() * 255);
const g = Math.floor(Math.random() * 255);
const b = Math.floor(Math.random() * 255);
newColor = r * 0.299 + g * 0.587 + b * 0.114
if(newColor > 186) {
newColor = 'black';
} else {
newColor = 'white';
}
return `rgb(${r}, ${g}, ${b})`;
}
I tried making my own function, I have tried putting an if statement on the outside of the function.