Good evening. We need a function that would compare all td cells for content.

For example, there are 5 cells. It is necessary that the logical if operator compares with an example and gives me an answer (true / false)

It is for certain clear that the written function below works not how it is necessary to me. I understand the principle of operation of this function, but I myself cannot write the one that I need for which day

 function proverka() { var myTable = document.getElementById("myTable"); for (var i = 0, row; row = myTable.rows[i]; i++) { for (var j = 0, col; col = row.cells[j]; j++) { if (myTable.rows[0].cells[i].innerText == myTable.rows[0].cells[i].innerText && myTable.rows[0].cells[i].innerText == "X") { cnsole.log("verj") } } } } 
  • What is the meaning of comparing myTable.rows[0].cells[i].innerText with yourself? And the variable j not used anywhere! - sercxjo
  • @sercxjo in nothing, this is a function here that was easy to show on it - Tigran

1 answer 1

It should work like this:

 var table = document.getElementById( "myTable" ); for ( var i = 0; i < table.rows.length; i++ ) { if (table.rows[i].cells[0].innerHTML === table.rows[i].cells[1].innerHTML) alert(true); else alert(false); } 
 <table id="myTable"> <tr> <td>aaa</td> <td>aaa</td> </tr> <tr> <td>bbb</td> <td>aaa</td> </tr> <tr> <td>aaa</td> <td>aaa</td> </tr> </table> 

Next yourself.

-------------------- ADDED ----------------------

 var table = document.getElementById( "myTable" ); for ( var i = 0; i < table.rows.length; i++ ) { for ( var j = 0; j < table.rows[0].cells.length; j++ ) { alert("indexes x:" + i + " y:" + j + " = " + table.rows[i].cells[j].innerHTML); } } 
 <table id="myTable"> <tr> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>o</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> </table> 

---------- ADDED ---------

 var table = document.getElementById( "myTable" ); for ( var i = 0; i < table.rows.length; i++ ) { for ( var j = 0; j < table.rows[0].cells.length; j++ ) { //alert("indexes x:" + i + " y:" + j + " = " + table.rows[i].cells[j].innerHTML); if (table.rows[i].cells[0].innerHTML === table.rows[i].cells[j].innerHTML) alert(true); else alert(false); } } 
 <table id="myTable"> <tr> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>o</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> </table> 

--------- ADDED ----------

 var table = document.getElementById( "myTable" ); var flag; // флаг проверки на победу for ( var i = 0; i < table.rows.length; i++ ) { for ( var j = 0; j < table.rows[0].cells.length; j++ ) { //alert("indexes x:" + i + " y:" + j + " = " + table.rows[i].cells[j].innerHTML); if (table.rows[i].cells[0].innerHTML === table.rows[i].cells[j].innerHTML) flag = true; // все хорошо else { flag = false; // ложь continue; // найдена ложь оборвать цикл и продолжить } } if (flag === true) alert("победа " + table.rows[i].cells[0].innerHTML + " в строке " + i); } 
 <table id="myTable"> <tr> <td>x</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>o</td> <td>x</td> <td>x</td> <td>x</td> <td>x</td> </tr> <tr> <td>o</td> <td>o</td> <td>o</td> <td>o</td> <td>o</td> </tr> </table> 

  • @perect I wrote this way, it is imperative to emphasize here that besides that their innerHTML are equal to each other, X or Օ must also be, and with empty cells, it will also give true, secondly, this way if you have more than 2 td in the table it will not work, since for example I have a table with six td, he will find two matches at each move and will return true to me, and the fact is that the value myTable.rows.length is arbitrary, you cannot write for three, four or five, the function should work everywhere - Tigran
  • @Tigran, formulate correctly the field comparison rules (there is not enough data for the algorithm), secondly, I only showed you the principle of table processing, but thirdly, see the full cycle of processing the entire table (I added my reply). - perfect
  • The whole question is this, I can write for three, four and as many times as I like, but this is only for a specific case, but how to write that it happened again n-but I won’t mind it - Tigran
  • table.rows[i].cells[0].innerHTML === table.rows[i].cells[1].innerHTML instead, I would like to write so that cells [0] compare with the following and so much so that the whole line Won't Check - Tigran
  • you just need to correctly create the task before the algorithm and that is what you want to get. What do you want to see in the end? for example, let's analyze one row of the table, if all the elements in the line are the same, then any element of the line can be compared with the first element. If you do not understand ask a question - perfect