Building a JavaScript grid with odd and even characters using two loops -
this first question on stackoverflow.
i have build gridgenerator(num)
. if num
3
, this:
#_# _#_ #_#
if num
4
, this:
#_#_ _#_# #_#_ _#_#
i able solve odd numbers, struggle adjust numbers.
function gridgenerator(num) { var grid = ''; var row = ''; (var = 0; < num; i++) { (var j = 0; j < num; j++) { if (row.length % 2) { row += '_'; } else { row += '#'; } } grid += row.slice(-num) + '\n'; } return grid; } console.log(gridgenerator(3));
need hint how solve 2
, 4
, , other numbers. thank you!
try if ((i+j) % 2)
function gridgenerator(num) { var grid = ''; var row = ''; (var = 0; < num; i++) { (var j = 0; j < num; j++) { if ((i+j) % 2) { row += '_'; } else { row += '#'; } } grid += row.slice(-num) + '\n'; } return grid; } console.log(gridgenerator(4));
Comments
Post a Comment