vendredi 6 octobre 2017

If/else statement doesn't work in jQuery as I think it should

I have the exercise to code a tic-tac-toe game. I was, just, trying some ideas when I came to this problem. My function doesn't work and it is very simple if/else statement.

function game(){
if(e = "x"){     
  d.text("o");      
   }else{
  c.text("o");      
   }  
  };

Whatever field I hit it returns "o" into d. Also when I change if(e = "x") to if(e == "x") it always returns "o" into c.

I will appreciate any kind of hint. Thank you.

$(document).ready(function() {
  var a = $("#a"),
    b = $("#b"),
    c = $("#c"),
    d = $("#d"),
    e = $("#e"),
    f = $("#f"),
    g = $("#g"),
    h = $("#h"),
    i = $("#i");

  function game() {
    if (e = "x") {
      d.text("o");
    } else {
      c.text("o");
    }
  };


  function humanIsX() {
    $(".field").on("click", function() {
      $(this).text("x");
      game();
    });
  };
  humanIsX();
});
.field {
  display: inline-block;
  height: 100px;
  width: 100px;
  font-size: 110px;
  text-align: center;
  border: 2px solid gray;
  border-radius: 5%;
}
<script src="http://ift.tt/1oMJErh"></script>
<div id="third_page">
  <div class="row" id="first-row">
    <div class="field" id="a"></div>
    <div class="field" id="b"></div>
    <div class="field" id="c"></div>
  </div>
  <div class="row" id="second-row">
    <div class="field" id="d"></div>
    <div class="field" id="e"></div>
    <div class="field" id="f"></div>
  </div>
  <div class="row" id="third-row">
    <div class="field" id="g"></div>
    <div class="field" id="h"></div>
    <div class="field" id="i"></div>
  </div>
</div>

Aucun commentaire:

Enregistrer un commentaire