Javascript conditional statement not executing -
i'm attempting make function builds checker board can't second conditional statement execute , create new line.
function grid(size) { var board = ""; (i = 0; <= (size * size); i++) { if (i % 2 === 0) { board += ' '; } else if (i % size === 0) { board += "\n"; } else { board += '#'; } } console.log(board); } grid(8); it appears else if statement not executing because i've tried changing '\n' , condition other things nothing printed. question eloquent javascript , given solution this:
var size = 8; var board = ""; (var y = 0; y < size; y++) { (var x = 0; x < size; x++) { if ((x + y) % 2 == 0) board += " "; else board += "#"; } board += "\n"; } console.log(board); i'm not sure why second 1 works mine doesn't.
let´s analyse code:
if(i % size === 0) this means, when equal 0 or multiple of size. in case, size 8.
so if trigger, should 0,8,16,32....
in cases, never fire because when 0,8,16,32... % 2 0, run first if instead , never second one. should inverse order of ifs or run both
Comments
Post a Comment