javascript - How to check if variables are true with in a multi-dimensional array? -


i trying call these arrays , check if whole array true (like ["cell1", "cell2", "cell3"]). have html , js below.

this pretty easy fix, struggling find stuff , how fix it.

<tr>             <td id="cell1" onclick="tic(this)"></td>             <td id="cell2" onclick="tic(this)"></td>             <td id="cell3" onclick="tic(this)"></td>         </tr>          <tr>             <td id="cell4" onclick="tic(this)"></td>             <td id="cell5" onclick="tic(this)"></td>             <td id="cell6" onclick="tic(this)"></td>         </tr>          <tr>             <td id="cell7" onclick="tic(this)"></td>             <td id="cell8" onclick="tic(this)"></td>             <td id="cell9" onclick="tic(this)"></td>         </tr>  var win = [             ["cell1", "cell2", "cell3"],             ["cell1", "cell4", "cell7"],             ["cell1", "cell5", "cell9"],             ["cell2", "cell5", "cell8"],             ["cell3", "cell6", "cell9"],             ["cell3", "cell5", "cell7"],             ["cell4", "cell5", "cell6"],             ["cell7", "cell8", "cell9"]         ];          function tic(element) {             element.textcontent = "x";              (i = 0; < win.length; i++) {                  var thiswinningcombo = win[i];                  (x = 0; x < thiswinningcombo.length; x++) {                      var cellid = thiswinningcombo[x];                     var cellvalue = document.getelementbyid(cellid).textcontent;                     console.log(cellid + ": " + cellvalue);                 }              }         } 

is want achieve? haswon variable set true if 1 of winning combos found

var win = [              ["cell1", "cell2", "cell3"],              ["cell1", "cell4", "cell7"],              ["cell1", "cell5", "cell9"],              ["cell2", "cell5", "cell8"],              ["cell3", "cell6", "cell9"],              ["cell3", "cell5", "cell7"],              ["cell4", "cell5", "cell6"],              ["cell7", "cell8", "cell9"]          ];            function tic(element) {              element.textcontent = "x";                (i = 0; < win.length; i++) {                   var thiswinningcombo = win[i];                  var haswon = true;                  (x = 0; x < thiswinningcombo.length; x++) {                       var cellid = thiswinningcombo[x];                      var cellvalue = document.getelementbyid(cellid).textcontent;                      //console.log(cellid + ": " + cellvalue);                      haswon = haswon && (cellvalue == "x");                  }                  if (haswon){                    console.log(haswon);                  }                }          }
<table border="1" cellpadding="10">  <tr>              <td id="cell1" onclick="tic(this)"></td>              <td id="cell2" onclick="tic(this)"></td>              <td id="cell3" onclick="tic(this)"></td>          </tr>            <tr>              <td id="cell4" onclick="tic(this)"></td>              <td id="cell5" onclick="tic(this)"></td>              <td id="cell6" onclick="tic(this)"></td>          </tr>            <tr>              <td id="cell7" onclick="tic(this)"></td>              <td id="cell8" onclick="tic(this)"></td>              <td id="cell9" onclick="tic(this)"></td>          </tr>  </table>


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -