Created
September 25, 2016 16:05
-
-
Save mojenmojen/b78dca67bb07802eb25eb6d6a03ea9fc to your computer and use it in GitHub Desktop.
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: # # # # # # # # # # # # # # # # # # # # # # # # # # #…
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
// Set the grid size | |
var size = 8; | |
// set a variable for the grid output string | |
var gridOutput = ""; | |
// set a variable for the row output string | |
var rowOutput = ""; | |
// set variables for the things that make up the grid | |
var gridSpace = " "; | |
var gridHash = "#"; | |
// Set an outer loop for the number of rows | |
for ( var row = 1; row <= size; row++ ) { | |
// if the row is odd, start with a space | |
if ( row%2 != 0 ) { | |
var currItem = gridSpace; | |
var nextItem = gridHash; | |
} | |
// otherwise start with # | |
else { | |
var currItem = gridHash; | |
var nextItem = gridSpace; | |
} | |
// reset the rowOutput variable | |
rowOutput = ""; | |
// Set an inner loop for the number of columns | |
for ( var column = 1; column <= size; column++ ) { | |
// build the row by adding each column | |
if ( column%2 != 0 ) | |
rowOutput += currItem; | |
else | |
rowOutput += nextItem; | |
} | |
// add the row to the grid output | |
gridOutput = gridOutput + rowOutput + "\n"; | |
} | |
//print the grid to the console | |
console.log( gridOutput ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment