最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Building a JavaScript grid with odd and even characters using two loops - Stack Overflow

programmeradmin2浏览0评论

This is my first question on StackOverflow.

I have to build gridGenerator(num). If num is 3, it would look like this:

#_#
_#_
#_#

If num is 4, it would look like this:

#_#_
_#_#
#_#_
_#_#

I was able to solve it for odd numbers, but struggle to adjust it to even numbers.

function gridGenerator(num) {
  var grid = '';
  var row = '';

  for (var i = 0; i < num; i++) {
    for (var j = 0; j < num; j++) {

      if (row.length % 2) {
        row += '_';
      } else {
        row += '#';
      }
    }
    grid += row.slice(-num) + '\n';
  }
  return grid;
}

console.log(gridGenerator(3));

This is my first question on StackOverflow.

I have to build gridGenerator(num). If num is 3, it would look like this:

#_#
_#_
#_#

If num is 4, it would look like this:

#_#_
_#_#
#_#_
_#_#

I was able to solve it for odd numbers, but struggle to adjust it to even numbers.

function gridGenerator(num) {
  var grid = '';
  var row = '';

  for (var i = 0; i < num; i++) {
    for (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 a hint how to solve it for 2, 4, and other even numbers. Thank you!

Share Improve this question edited Jul 26, 2017 at 2:11 Yunielf asked Jul 26, 2017 at 1:36 YunielfYunielf 971 silver badge6 bronze badges 4
  • The first row starts off with #, and then alternates (so #_#), and then the next row starts with _ (_#_). That might help. – Qwerp-Derp Commented Jul 26, 2017 at 1:53
  • Do you want it for a general case? I have a solution but don't want to spoil your fun. – Jarek Kulikowski Commented Jul 26, 2017 at 1:56
  • This is one of the exercises for the boot camp preps. – Yunielf Commented Jul 26, 2017 at 1:59
  • Here's a hint. Describe the cases where you would place a "#" in terms of the oddness and even-ness of the row and column it goes into. Now ask if your code implements the analytical solution you came to. – traktor Commented Jul 26, 2017 at 2:02
Add a ment  | 

3 Answers 3

Reset to default 5

Try this if ((i+j) % 2)

function gridGenerator(num) {
  var grid = '';
  var row = '';

  for (var i = 0; i < num; i++) {
    for (var j = 0; j < num; j++) {
      if ((i+j) % 2) {
        row += '_';
      } else {
        row += '#';
      }
    }
    grid += row.slice(-num) + '\n';
  }
  return grid;
}

console.log(gridGenerator(4));

You can use the condition num % 2 to determine if a number is even or odd. I would use two loops like you are doing. Make your character addition based on the even / odd state of the row and column. At the end of each row insert the line break.

EDIT: Here you go.

function generateGrid( num ) {
  let i, j, grid = "";
  
  for ( i = 0; i < num; i++ ) {
    for ( j = 0; j < num; j++ ) {
      if ( ( i + j ) % 2 ) {
       	grid += "_";
      } else {
      	grid += "#";
      }
    }        
    grid += "\n";
  }      
  return grid;
}

var grid = generateGrid( 4 );

console.log( grid );

function gridGen(num) {
    var even = '';
    for (var i = 0; i< num ; i++)
        even += (i%2) ? '_' : '#';

    odd = even.substring(1) + (num%2 ? '_' : '#');
    var out = '';
    for (var i = 0; i< num ; i++)
        out += ((i%2) ? odd : even) + '\n';
    return out;
}
console.log('Even Case');
console.log( gridGen(8));
console.log('Odd Case');
console.log( gridGen(7));

If you are looking for another approach + efficiency try this

发布评论

评论列表(0)

  1. 暂无评论