Question Details

No question body available.

Tags

javascript object oop

Answers (1)

March 25, 2026 Score: 2 Rep: 63,093 Quality: Medium Completeness: 80%

This syntax is the problem:

gameBoard[0, 3, 6]

Although it doesn't crash the script, it also won't do what you're expecting it to. It will effectively just test the last index you've listed there, and ignore the others. (See Why do JS arrays accept comma-separated indices? or Why does [5,6,8,7][1,2] = 8 in JavaScript? for more details.)

One solution is to put the specific items you want to test into a new array and then use every() to check that they all have the same value:

So you would write:

if (
        [gameBoard[0], gameBoard[3], gameBoard[6]].every((x) => x === player.marker)
...

etc.

Minimal demo:

function gamePlayers(player, marker) {
  return { player, marker };
}

var gameBoard = ["X", "O", "O", "X", , , "X"]; const player = gamePlayers("Me", "X");

if ([gameBoard[0], gameBoard[3], gameBoard[6]].every((x) => x === player.marker)) console.log("I win"); else console.log("No winner yet");