Javascript if Statement Question 1

Chessboard

Create a function which returns a string that represents an 8 by 8 grid, using newline characters to separate lines.

At each position of the grid there is either a space or a '#' character.

The characters should resemble a chessboard.



var gridSize = 8,
    chessBoard = "";

for (var line = 0; line < gridSize; line++) {
  
  for (var square = 0; square < gridSize; square++) {
    if ((line + square) % 2 === 0) {
      chessBoard += "#";
    } else {
      chessBoard += " ";
    }
  }

  chessBoard += "\n";
}
 
// Output (8)
console.log(chessBoard);



PreviousNext

Related