Skip to content

Instantly share code, notes, and snippets.

@stahnni
Created May 13, 2017 07:39
Show Gist options
  • Save stahnni/1b26f9c48595d17898ba67a5b70b6ec4 to your computer and use it in GitHub Desktop.
Save stahnni/1b26f9c48595d17898ba67a5b70b6ec4 to your computer and use it in GitHub Desktop.
Chess board
/*
Chess board
Write a program that creates a string that represents an 8×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 form a chess
board.
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
When you have a program that generates this pattern, define a variable
size = 8 and change the program so that it works for any size, outputting
a grid of the given width and height.
*/
let boardSize = 8;
let boardLine = "";
for(let i = 0; i < boardSize; i++){
for (let j = 0; j < boardSize; j++){
if ((j+i) % 2 === 0){
boardLine+="#";
} else {
boardLine+=" ";
}
}
boardLine += "\n";
}
console.log(boardLine);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment