Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active April 26, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/0b0e1370673a8aaa3e995f78747432a1 to your computer and use it in GitHub Desktop.
Save McLarenCollege/0b0e1370673a8aaa3e995f78747432a1 to your computer and use it in GitHub Desktop.
Exercise : X win in board

Given the following board variable representing a tic-tac-toe board. Write a function that takes a board as an input and return true if X has won and false if X has not won.

Note: We can give you any state of the board and you will have to give correct result if X has won for that particular state of the board.

Hint: You have to check in all rows, columns and diagonals.

CODE TEMPLATE


function hasXWon(board)
{
  //write function body to check if x has won or not.
}
let board = [[' ', 'O', 'X'],['O', 'X', ' '], ['O', ' ', 'X']];
console.log(hasXWon(board));// false
board = [['X', 'O', ' '],['O', 'X', 'O'], ['X', 'O', 'X']];
console.log(hasXWon(board)); // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment