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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -