scrimba
Tic Tac Toe
calculateWinner function explained
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

calculateWinner function explained
AboutCommentsNotes
calculateWinner function explained
Expand for more info
helpers.js
run
preview
console
export function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}

const squares = [
null, null, null,
'X', 'X', 'O',
null, null, null
];

console.log(calculateWinner(squares));
Console
null
,
/index.html
-3:49