Last active
July 27, 2023 17:50
-
-
Save sawant/c22f022829ec576ef255 to your computer and use it in GitHub Desktop.
JavaScript Chess Board
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
/* | |
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