admin管理员组文章数量:1022793
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!
-
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
3 Answers
Reset to default 5Try 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
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!
-
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
3 Answers
Reset to default 5Try 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
本文标签: Building a JavaScript grid with odd and even characters using two loopsStack Overflow
版权声明:本文标题:Building a JavaScript grid with odd and even characters using two loops - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572052a2156792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论