As a beginner of javaScript coding I wrote some code and when I was trying to run the code it was executing and went to the else
block every time instead of the if
block. I found where the problem was occurring but I didn't know how to solve it. I got spaces between the numbers when I clicked the color, yet the else
statement was always executing. You can see my problem below to get some idea. How can I make this code work? Please help me, thanks in advance.
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[4];
for(var i=0;i<squares.length;i++){
squares[i].style.background=colors[i];
squares[i].addEventListener("click",function(){
var clickedColor=this.style.back
console.log("clicked color is"+clickedColor);
console.log("picked color is "+pickedColor);
if(clickedColor===pickedColor)
{
alert("correct");
}
else
{
alert("wrong");
}
})
}
The Problem: The rgb below was not matched and spaces were added. How can we remove those spaces?
clicked color is rgb(0, 0, 255)
, picked color is rgb(0,0,255)