Skip to content

Instantly share code, notes, and snippets.

@edu222
Last active March 13, 2017 18:52
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 edu222/04085c3cdbb8026bd5e8c1c5f0073457 to your computer and use it in GitHub Desktop.
Save edu222/04085c3cdbb8026bd5e8c1c5f0073457 to your computer and use it in GitHub Desktop.
Chess Board Exercise
/*
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.
*/
//Solution 1
board = '';
for (var i = 0; i < 8; i++)
{
for(var j=0; j < 8; j++)
{
if( ( (i + j) % 2) == 0)
{
board += ' '
}
else
{
board += '#'
}
}
board += '\n'
}
console.log(board);
//Solution 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment