0

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>

2
  • This is why it's generally a bad idea to mix presentation and data. A better approach would be to attach the click handler only to the correct div and not all of them. Commented Oct 20, 2017 at 5:48
  • 2
    As the answer below suggests you were doing 'rgb(0, 255, 255)' === 'rgb(0,255,255)'. Could have been a simple catch if you had included pickedColor along side clickedColor console.log Commented Oct 20, 2017 at 5:51

2 Answers 2

3

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>

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)"
]

I just added blank..

Sign up to request clarification or add additional context in comments.

2 Comments

where did you add the blank? @Young Kyun Jin
@Zack Your typing => rgb(255,0,0); My typing => rgb(255, 0, 0);
0

In the if condition you are comparing the two different strings. When you apply this color in rgb() format, the browser formats it with spaces between the color arguments.

You gave: rgb(0,255,0)
In browser: rgb(0, 255, 0)

So, you need to add spaces in your colors array. Change your colors array to this,

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)"
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.