# Compare Values of 2 arrays for TicTacToe

Hello Everyone! can you please help me to solve this algorithm…

My goal is to iterate 2 Arrays and compare them to find out if values from a single Array like: [0, 2, 1] are the same to one of this Sub Arrays: [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6],…
On this case it should return true bacause [0, 2, 1] and [0, 1, 2] have the same values: 0, 1, 2.

Here is an Exemplo code to this problem:

``````let clicksPlayerO = [0, 4, 3];

const gameWinner = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[6, 4, 2]
];

if(clicksPlayerO.length >= 2){
console.log('Array length: ' + clicksPlayerO + ' length is bigger or iqual than 2!');
//is some of this values: [0, 2, 1] equal to this one of arrays values: [0, 1, 2], [3, 4, 5],  [6, 7, 8], [0, 3, 6], [3, 4, 5]
// this should be true because both array has same numbers:  [0, 2, 1] and this [0, 1, 2]
}else{
console.log('Array length: ' + clicksPlayerO + ' length is smaller than 2!');
}
``````

This logic I would like to use for a TicTacToe game.
Here is the Link where I currently worrking :

https://codepen.io/Alanes/pen/WMaBMB?editors=1111

Pseudo code.
iterate over 1st array.
if value exists in 2nd array, ok continue checking
else fail - not a match
repeat until all values are found in 2nd array.

Note, you have and array of arrays so each one needs checking via another iterator (loop).

Thank you Johnny!

If i do like following Code:

``````let clicksPlayerO = [0, 8, 1];

const gameWinner = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
];

if(clicksPlayerO.length >= 2){
for(let i = 0; i < gameWinner.length; i++){
//console.log(gameWinner[i]);
for(var j = 0; j < gameWinner[i].length; j++){
//console.log(gameWinner[i][j]);
if (gameWinner[i][j] === clicksPlayerO[0]) {

console.log(gameWinner[i][j] + ' matches');

}else if(gameWinner[i][j] === clicksPlayerO[1]){

console.log(gameWinner[i][j] + ' matches!');

}else if(gameWinner[i][j] === clicksPlayerO[2]){

console.log(gameWinner[i][j] + ' matches!');
}else{
console.log('no matches!');
}
}
}
}
``````

I’m doing somthing wrong and I can see it…
It returns always true, because the second for Loop get all Values from const gameWinner Array together and then compare. Of course it will be true
But what I want to do with the second Loop is: get every sub array and compare every 3 Values inside of every sub Array . And see if them are same values to my let clicksPlayerO = [0, 8, 1]

I recommend using .indexOf() function to test for the presence of an item in the array. Then you won’t need so many loops.