I'm working on a simple Tic Tac Toe game in Javascript. Right now, to check for a winner, I have a function that first gets whose turn it is and then finds wherever they have an X or an O on the board. The spaces they have are numbered (1-8) and added to an array. So far so good.
Now I'm trying to compare another array, the array with all of the winning combinations:
var winningCombinations = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]];
with a test array, that should come up a winner as it contains 2, 5, and 8.
Source of my test:
<script>
var test = [2,4,5,8]
var winningCombinations = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]];
for(var x=0; x<winningCombinations.length; x++) {
if (winningCombinations[x].indexOf(test) > -1) {
alert("Win!");
} else {
alert ("No win.");
}
}
</script>
I think right now it's only testing for [2,4,5,8] as an entire value--not for an instance of the individual numbers inside. This is where I'm stumped. How can I check to see if the test array, in any order, matches any of the winningCombinations values?