I am making a tic tac toe game. Each square on the board has an index from 0 to 8. To check the winner, I have a two-dimensional array winningPlays that contains all potential winning combinations. I have two arrays that contain plays by Xs or by Os - xPlays and oPlays, respectively. After sorting xPlays, I want to compare it to winningPlaysto see if any of the arrays match. If they do, I want to console.log('X wins'). I can't seem to find a way to execute the console.log at the right time to determine the winner. Here is the piece of problem code:
const winningPlays = [
[0,1,2], //across top
[3,4,5], //across middle
[6,7,8], //across bottom
[0,3,6], //left down
[1,4,7], //middle down
[2,5,8], //right down
[0,4,8], //top left to bottom right
[2,4,6] // top right to bottom left
]; //length == 8
function checkForWinner() {
for(let i = 0; i < winningPlays.length; i++){
for(let j = 0; j < winningPlays[i].length; j++){
if (xPlays.length < 3) {
return;
} else if (winningPlays[i][j] !== xPlays[j]) {
console.log(winningPlays[i][j])
console.log(xPlays[j])
return;
}
console.log('win') // executes every time that xPlays.length >= 3
}
}
};
And here is a link to my codepen draft: https://codepen.io/CDLWebDev/pen/gOawjvE
xPlays.lengthcheck shouldn't be inside the loop.forloop.