lundi 12 avril 2021

How to use if/else in a math.random function?

how can i make my button color change into specific color (white/'ffffff') IF the random function catch this specific color ('#8675a9')?

const colButton = document.querySelector('.col')
const button1 = document.querySelector('.check')
const button2 = document.querySelector('.next')
const button3 = document.querySelector('.hide')
const terma = document.querySelector(".term");
const body = document.querySelector('body')
const colors = ['#8675a9', '#f5cab3', '#d1f5d3', '#bad7df']

this is the code where i tried to make the function with if statement:

body.style.backgroundColor = 'white'
colButton.addEventListener('click', changeBackground)

function changeBackground(){
const colorIndex= parseInt(Math.random()*colors.length)
if ([colorIndex]= '#8675a9') {
 button1.style.backgroundColor = '#ffffff';
 button1.style.color = colors[colorIndex];
} else {
body.style.backgroundColor = colors[colorIndex]
button1.style.color = colors[colorIndex];
button1.style.backgroundColor = '#0f0f0f';
button2.style.color = colors[colorIndex];
button2.style.backgroundColor = '#0f0f0f';
button3.style.color = colors[colorIndex];
button3.style.backgroundColor = '#0f0f0f';
colButton.style.color = colors[colorIndex];
colButton.style.backgroundColor = '#0f0f0f';
terma.style.color = colors[colorIndex];
}
}



Aucun commentaire:

Enregistrer un commentaire