Skip to content

Instantly share code, notes, and snippets.

@sawant
Last active July 27, 2023 17:50
Show Gist options
  • Save sawant/c22f022829ec576ef255 to your computer and use it in GitHub Desktop.
Save sawant/c22f022829ec576ef255 to your computer and use it in GitHub Desktop.
JavaScript Chess Board
/*
http://eloquentjavascript.net/02_program_structure.html
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.
*/
var size = 8, string = "";
for (var line = 0; line < size; line++) {
for(var i = 0; i < size; i++) {
string += ((i+line) % 2) === 0 ? "#" : " ";
}
string += "\n";
}
console.log(string);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment