# Iterate 2 dimensional arrays to compare with a list Items index

hello everyone! can please somebody help me to understand how to compare 2 dimensional Arrays value with a Index of li Elements. This Exemplo I will like to use for my TicTacToe game.

``````//here Select my List Elements:
let liList = document.querySelectorAll('.boxes li');

//and here the 2 dimensional arrays with possible combinations to win the game
const horizontalBoxes = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
];
const vertical = [
[0, 3, 6],
[3, 4, 5],
[6, 7, 8]
];
const diagonal = [
[0, 4, 8],
[6, 4, 2]
];

``````

so now I have:
variable liList which has this output a NodeList(9) [li.box, li.box, li.box, li.box, li.box, li.box, li.box, li.box, li.box]_ and the 2 dimensional Arrays

Now what I want to do is:
to proof if those index : [li.box, indexli.box, li.box, li.box, li.box, li.box, li.box, li.box, li.box] are the same to one of my 2 dimensional array.

Like this:

``````Index of my li list: 0 , 4, 8  is equal  horizontalBoxes [[0, 1, 2]] if yes display winner

if not compare 0 , 4, 8 again with next array:  horizontalBoxes [[3, 4, 5]] and so on...
``````

Here you find a small code of what i mean and trying to do: https://codepen.io/Alanes/pen/WMaBMB?editors=1111

First, I would suggest having only one two-dimensional array which contained the sub-arrays that are contained in the three separate two-dimensional arrays. That way, you only need a single loop.

Second, I suggest having another data structure ( I suggest a one-dimensional array representing the board squares) to track the current state of each square (after a click). A state would either be empty, player, or computer. You can figure out how best to mark them, but your logic could be to iterate through each sub array of the two-dimensional array (described at the top) and check if all of a sub array’s elements have the same state value of the indices of the one-dimensional board array. If they are all the same, then you can determine the winner based on the state value (player mark vs computer mark).

Hopefully, this will be enough to get you started on a proper algorithm. If you have any questions or want to share your written algorithm for more suggestions/hints, then just post it with your questions.

Looking at your current code, you are not tracking each square’s state. You do push an li element into saveIndex if player == 1, but I am not seeing how that is going to help you.

Thank you very much!
Yes, definitely your suggest helps!

You already have atopic with the exact same question. Instead of asking the same question again, just wait till someone responds to your other post. I will delete this question here, so we don’t end up with different people providing answers on two different threads for the same question.

You were given some good advice in the other thread by a fellow camper. In general, you don’t seem to have a solid algorithm defined or at least not what I can tell from the code you have written. Update the other thread with your written algorithm. It can be pseudo-code or plain language as long as it details each step in your algorithm. We can give you advice on your written algorithm which will ultimately help you write the correct code.