this is the javascript and HTML and CSS I m very new to javascript the problem is if statement is always showing wrong even though it should not if statement is always false even if I click on the value of the color of win variable that the value of the 3rd index of the array colors its showing wrong
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 win =colors[3];
var winclrdisplay=document.querySelector(".winclr");
var square= document.querySelectorAll(".square");
winclrdisplay.textContent=" "+ win;
for (var i = 0 ;i < square.length ; i++){
//colors
square[i].style.background =colors[i];
// click listener
square[i].addEventListener("click", function(){
var click = this.style.background;
if (click == win){
alert("correct");
}else{
alert("wrong");
}
}
);
}
body {
background-color: #232323;
}
.square{
width: 30%;
background: purple;
padding-bottom: 30%;
float: left;
margin: 1.66%;
}
#container {
max-width: 600px;
margin: 0 auto;
}
h1 {
color:white;
}
<html>
<head>
<title>color guessing game</title>
<link rel="stylesheet" type="text/css" href="colorapp.css">
</head>
<body>
<h1>The Great <span class="winclr"></span> colour 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 type="text/javascript" src="colorapp.js"></script>
</body>
</html>