Q:

What is the best way to check if values multiple values in certain ways match?

I have to make a tic tac toe game, and for it to work I need to have a function to check if there are 3 in a row.
the grid is like this
012
345
678

It means 012, 345, 678, 036, 147, 258, 048, 246 are all possible combinations.
I tried:  
if(clickedCells[0] && clickedCells[1] && clickedCells[2] !== "" && clickedCells[0] === clickedCells[1] && clickedCells[0] === clickedCells[2]){
        done();
}

But it doesn't work because it is invalid.
I would like to know the best way to get such a thing working, because I have never had to so much if-statements in a row and it looks like a total mess if I check each thing independently.

A:

You can store every valid combination in an array.
var combinations = new Array([0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]);

Now instead of setting up an if-condition for a single combination you can loop over the entire array and check your board at the position specified.
if (board[combinations[a][0]] == id && board[combinations[a][1]] == id && board[combinations[a][2]] == id)

Here's the compelte example:

var board = new Array(
  1, 0, 0,
  0, 1, 0,
  0, 0, 1
);

function check(id) {
  var ret = null;
  var combinations = new Array([0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]);
  var found;
  for (var a = 0; a < combinations.length; a++) {
    if (board[combinations[a][0]] == id && board[combinations[a][1]] == id && board[combinations[a][2]] == id) {
      ret = combinations[a];
      break;
    }
  }
  return ret;
}
console.log(check(1));

