Last active
March 13, 2017 18:52
-
-
Save edu222/04085c3cdbb8026bd5e8c1c5f0073457 to your computer and use it in GitHub Desktop.
Chess Board Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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