The new encoder here is trying to recognize JS. I have already done codecademy and am currently working on Eloquent Javascript. I finally got something together, scratching my head for a very long time ... but it won’t work! I'm not quite sure if I'm approaching this at right angles, but I know that I want to use loops to track progress through grid printing #.
Write a program that creates a line that represents an 8 × 8 grid, using newline characters to separate lines. In each position, the grid is either a space or the symbol "#". Characters should form a chessboard. Passing this line to console.log should look something like this:
#
My code is below:
var chessBoard = ""; var size = 8; for (var lineCounter = 1; lineCounter < size; lineCounter++) { if (lineCounter%2 === 0) { / /if lineCounter is an even number for (var charCounter = 1; charCounter < size; charCounter++) { var evenOdd = (charCounter%2 === 0); switch (evenOdd) { case true: (chessBoard += "#"); break; case false: (chessBoard += " "); break; } } } else { //if lineCounter is an odd number for (var charCounter = 1; charCounter < size; charCounter++) { var evenOdd = (charCounter%2 === 0); switch (evenOdd) { case true: (chessBoard += " "); break; case false: (chessBoard += "#"); break; } } } chessBoard += "\n"; } console.log(chessBoard);
The current output of the program is as follows:
#
After a few iterations, I already learned a lot, but right now I see an error - I obviously reached the 7x7 grid instead of the 8x8 that I wanted to get. I suspect this is due to me using "<" in my loops, but not sure if there is a better way to handle this, and not just add an extra digit.
source share