Trying to make a Color Guessing game using JS.Console does not show me any errors, but it warns me "Wrong!". even when I choose the right number, when he needs to warn “Right!” ... I tried to solve this problem for about three hours!
If I delete else{ alert ("Wrong!") }and then click on the color, there will be no callback at all. Also, if I type the console square[i].style.background, it will return me
undefined
Here is my code:
var colors = [
"rgb(255,0,0)",
"rgb(255,255,0)",
"rgb(0,255,0)",
"rgb(0,255,255)",
"rgb(0,0,225)",
"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++){
squares[i].style.background = colors[i];
squares[i].addEventListener ("click",function() {
var clickedColor = this.style.background;
if(clickedColor === pickedColor){
alert("Correct!");
} else {
alert("Wrong!")
}
});
}
And here is my html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Color Game</title>
<link rel="stylesheet" type="text/css" href="colorGame.css">
</head>
<body>
<h1>The Great <span id="colorDisplay">RGB</span> Color Game</h1>
<div id="container">
<div class= "square"></div>
<div class= "square"></div>
<div class= "square"></div>
<div class= "square"></div>
<div class= "square"></div>
<div class= "square"></div>
</div>
<script src="colorGame.js"></script>
</body>
</html>
source
share