Skip to content

Instantly share code, notes, and snippets.

@teckliew
Created April 27, 2015 07:07
Show Gist options
  • Save teckliew/4c32189720bc26e7b1dc to your computer and use it in GitHub Desktop.
Save teckliew/4c32189720bc26e7b1dc to your computer and use it in GitHub Desktop.
Creating a JS checker board grid
//size is the dimension of the square grid
var size = 8,
white = " ",
black = "#",
rowE = "",
rowO = "",
grid = "";
//even grid row
for ( var x = 0 ; x < size; x++){
if (x % 2 === 0){
rowE = rowE + black;
}else{
rowE = rowE + white;
};
}
//odd grid row
for ( var i = 0 ; i < size; i++){
if (i % 2 === 0){
rowO = rowO + white;
}else{
rowO = rowO + black;
};
}
//combining row with '\n'
for ( var j = 0 ; j < size; j++){
if (j % 2 === 0){
grid = grid + rowO + "\n";
}else{
grid = grid + rowE + "\n";
};
}
console.log(grid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment