I am trying to run this code and everything seems to be working except the if statement. Even though there is a match but it still does not display the correct answer as shown in the code but instead displays the code in else statement.
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.querySelector("#colorDisplay");
colorDisplay.textContent = pickedColor;
for (var i = 0; i < colors.length; i++) {
//add initial colors to squares
squares[i].style.background = colors[i];
//add event listener
squares[i].addEventListener("click", function() {
//get color of picked square
var clickedColor = this.style.background;
//compare to the pickedColor
console.log(clickedColor);
if (clickedColor === pickedColor) {
alert("COORREECCTT");
} else {
alert("WRROONGG!!");
}
});
}
body {
background-color: #232323;
}
.square {
width: 30%;
background-color: purple;
padding-bottom: 30%;
float: left;
margin: 1.66%;
}
#container {
max-width: 600px;
margin: 0 auto;
}
h1 {
color: #fff;
}
<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>
</body>
'rgb(0, 255, 255)' === 'rgb(0,255,255)'. Could have been a simple catch if you had includedpickedColoralong sideclickedColorconsole.log