How do I solve the Eloquent Javascript "Chess Board"? -


new coder here trying learn js. did codecademy , working through eloquent javascript now. got after scratching head long while... doesn't work! i'm not quite sure if i'm approaching right angle know want use loops track progress through printing of # based grid.

write program creates string represents 8×8 grid, using newline characters separate lines. @ each position of grid there either space or “#” character. characters should form chess board. passing string console.log should show this:

 # # # # # # # #   # # # # # # # #   # # # # # # # #  

my code below:

    var chessboard = "";     var size = 8;      (var linecounter = 1; linecounter < size; linecounter++) {           if (linecounter%2 === 0) { /  /if linecounter number         (var charcounter = 1; charcounter < size; charcounter++) {             var evenodd = (charcounter%2 === 0);             switch (evenodd) {                 case true:                     (chessboard += "#");                     break;                 case false:                     (chessboard += " ");                     break;                 }             }                            }     else { //if linecounter odd number         (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 program this:

# # # #  # # #  # # # #  # # #  # # # #  # # #  # # # # 

through iterations, i've learned lot, right see error - i'm down 7x7 grid instead of 8x8 wanted get. suspect has me using "<" in loops, not sure if there's better way tackle instead of adding digit.

it's pretty easy need make 2 loops, 1 each row , other choosing element want console.log (either ' ' or '#').

check comments through solution

var size = 8; //this variable setting  var board = "";//this empty string we're going add either ' ' , '#' or newline  (var y = 0; y < size; y++) {   /*in outer loop add newline seperate rows*/   (var x = 0; x < size; x++) {/*every inner loop rappresents line, , alternatively it's adding either ' ' or '#' string that's being populated*/     if ((x + y) % 2 == 0)       board += " ";     else       board += "#";   }   board += "\n"; }  console.log(board); 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -