1

I am trying to match the user input array (this.scoreO, or this scoreX) arrays to the this.winningBoard array, but not with exact values. The values might not be in order, and I want to match the set of three even if there are more values in the "score" array.

So for example, maybe

this.scoreO = [9,5,2,1]

I would like it to trigger a match with [1,5,9] see below array

this.winningBoard = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[3,5,7],**[1,5,9]**]

Here is the code:

function Game (player) {

this.winningBoard = [
    [1,2,3],
    [4,5,6],
    [7,8,9],
    [1,4,7],
    [2,5,8],
    [3,6,9],
    [3,5,7],
    [1,5,9]
]   
this.scoreO = [];
this.scoreX = [];

}

Game.prototype.findWinner2 = function() {

for(i = 0; i < this.winningBoard.length; i++) {

    if (this.winningBoard[i].includes(this.scoreO) === true) {

        //display player 1 "O" is the winner


    } else if (this.winningBoard[i].includes(this.scoreX) === true) {

        //display player 2 "X" is the winner


    } 
}
}

I keep getting a false outcome.

1 Answer 1

1

You could do this with a combination of some, every, and includes (if you can use es6):

var winningBoard = [
  [1,2,3],
  [4,5,6],
  [7,8,9],
  [1,4,7],
  [2,5,8],
  [3,6,9],
  [3,5,7],
  [1,5,9],
];

var score0 = [9,5,2,1];

console.log(winningBoard.some(board => board.every(x => score0.includes(x))))

You can read this as: see if there is some element of winningBoard such that for every item in that element, score0 includes that item.

EDIT: Some added explanation (from the inside out):

x => score0.includes(x)

This is a function that returns true if the array score0 has x as an element. That one is pretty straightforward. In this case, x is just a number.

board => board.every(x => score0.includes(x))

every returns true only when it's callback is true for every element of the array. So since board is one of your winning number arrays (eg: [1,5,9]), this part will be true if every number in that board is included in score0.

Finally: winningBoard.some(board => board.every(x => score0.includes(x)))

some, being sort of like the inverse of every, returns true if the callback function returns true for any of the array elements. So this is true if any of the boards have all their elements included in score0, which is exactly the condition you want to check.

Last: the syntax x => x*2 is a shorthand for writing function(x) { return x*2 } (with some scope differences, not important here). You can read about arrow functions here.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, thanks CRice. Works well. If you could explain a little deeper that would be great. it works, but I don't fully understand what you did with board and x ( these are arrow functions?)
@Leslie I edited the post to include some extra explanation (I admit I took some things for granted originally). If anything is still unclear, please don't hesitate to ask for further clarification. Glad I could help. Thanks!
You rock @CRice! Very helpful - Thanks a bunch! I tried to upvote you but I'm still a pion with too little reputation.
@Leslie If this answered your question to your satisfaction I would very much appreciate an accept and a vote. ;) You should still be able to vote with low rep even if it doesn't affect the visible score.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.