mardi 21 février 2017

JavaScript if statement comparison not working

The if statement is supposed to compare buttonId value to randButtonId value and even when they do match it still says that it is wrong. The buttons go left to right button1, button2, button3, button4.

setBoard();
var randButtonId;
function setBoard(){
  var r = randomNumber(0 , 235);
  var g = randomNumber(0 , 235);
  var b = randomNumber(0 , 235);
  var color = rgb(r, g, b);
  var r2 = r + 20;
  var g2 = g + 20;
  var b2 = b + 20;
  var diffColor = rgb(r2 , g2 , b2);
  var randButtonId = "button" + randomNumber(1,4);
  setProperty("button1", "background-color", color);
  setProperty("button3", "background-color", color);
  setProperty("button2", "background-color", color);
  setProperty("button4", "background-color", color);
  setProperty(randButtonId, "background-color", diffColor);
  console.log("The correct one is: " + randButtonId);
}

function checkCorrect( buttonId ){
  console.log("Checking: " + buttonId);
  if( buttonId == randButtonId ) {
        console.log("You got it right!");
  } else {
        console.log("WRONG");
  }
  setBoard();
}
onEvent("button1", "click", function() {
  checkCorrect("button1");
});
onEvent("button2", "click", function() {
  checkCorrect("button2");
});
onEvent("button3", "click", function() {
  checkCorrect("button3");
});
onEvent("button4", "click", function() {
  checkCorrect("button4");
});

Aucun commentaire:

Enregistrer un commentaire