i'm trying to compare two RGB color that are store in variables, its a guess game. There are 6 squares with 6 different colors. So when the user click the right box it must say correct. But it always alert the second option. What am i doing wrong?? please this is the JavaScript code:
var colors = [
"rgb(255,0,0)",
"rgb(255,255,0)",
"rgb(0,255,0)",
"rgb(0,255,255)",
"rgb(0,0,255)",
"rgb(255,0,255)"
];
var squares = document.querySelectorAll(".square");
var pickedColor = colors[3];
var colorDisplay = document.getElementById("colorDisplay");
colorDisplay.textContent = pickedColor;
for (var i = 0; i < squares.length; i++) {
//add initinal colors to squares
squares[i].style.background = colors[i];
//add the click listener to the squares
squares[i].addEventListener("click", function () {
var clickedColor = this.style.background;
if (clickedColor === pickedColor) {
alert("Correct!");
} else {
alert("Incorrect!");
}
});
}
Aucun commentaire:
Enregistrer un commentaire