I've completed the small challenge in the eloquent JS book to create a chess board pattern:
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. 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.
I've used a function instead of a variable, but what do you think about my little script?
function chessboard(size) {
var output = "";
for(var i = 0; i < size; i++) {
for(var j = 0; j < size / 2; j++) {
if(i % 2 === 0) {
output += " ";
output += "#";
} else {
output += "#";
output += " ";
}
}
output += "\n";
}
return output;
}
console.log(chessboard(8));