I am currently working on a mini game project using HTML, CSS, and JavaScript.
Everything seems to be running smoothly with the HTML, CSS, and most of the JavaScript code. However, when I test the program in FireFox and attempt to click on a square div that matches the RGB value stored in a variable, it keeps giving the wrong answer. Instead of displaying "Correct" as expected, all the alerts are showing "Wrong".
This is the structure of the HTML file:
<!DOCTYPE>
<html>
<head>
<title>Color Game</title>
<link rel="stylesheet" type="text/css" href="colorGame3.css">
</head>
<body>
<h1>
RGB Color Game
<span id = "colorDisplay">
RGB
</span>
</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="colorGame3.js"></script>
</body>
</html>
Here's how the CSS file looks:
body{
background-color: #232323;
}
.square{
width:30%;
background:purple;
padding-bottom:30%;
float:left;
margin:1.66%;
}
#container{
max-width:600px;
margin:20px auto;
}
This is what the JavaScript file contains:
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[2];
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");
}
});
}