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

javascript - Building triangle with nested loops - Stack Overflow

programmeradmin1浏览0评论

I'm trying to build a triangle using nested loops.

var line, triangle;
line = triangle = "";

for (var row = 1; row <= 7; row++) {
  for (var x = 0; x < row; x++) {
    line += "#";
  }
  triangle += line + "\n";
}

console.log(triangle);

I'm trying to build a triangle using nested loops.

var line, triangle;
line = triangle = "";

for (var row = 1; row <= 7; row++) {
  for (var x = 0; x < row; x++) {
    line += "#";
  }
  triangle += line + "\n";
}

console.log(triangle);

I expected that each row is more than the last by only one "#" like this:

But this is the result I got:

Share Improve this question asked Dec 7, 2017 at 20:11 Moaaz BhnasMoaaz Bhnas 1,1897 gold badges20 silver badges41 bronze badges 3
  • 1 You probably want to start with an empty line. – SLaks Commented Dec 7, 2017 at 20:12
  • 1 You shouldn't need two loops for this. Keep two variables, one for the result and another for the current row. Append the current row to the result each loop – will.fiset Commented Dec 7, 2017 at 20:15
  • @will.fiset I'll try that – Moaaz Bhnas Commented Dec 7, 2017 at 20:26
Add a ment  | 

6 Answers 6

Reset to default 2

The reason your code does it is you are updating line on each iteration and you keep appending to it. If you want to do the nested loops, than you need to reset the variable line each time you are in the outer loop.

var line, triangle;
line = triangle = "";

for (var row = 1; row <= 7; row++) {
  line =""
  for (var x = 0; x < row; x++) {
    line += "#";
  }
  triangle += line + "\n";
}

console.log(triangle);

Or you can keep what you have and dump the inner loop and every iteration you just add one character to the line.

var line, triangle;
line = triangle = "";

for (var row = 1; row <= 7; row++) {
  line += "#";
  triangle += line + "\n";
}

console.log(triangle);

You need to empty your line before each nested iteration. Without this you have one line and every time concatenate new items to it. Also you can leave the line variable and just use the triangle.

var triangle = '';

for (var row = 1; row <= 7; row++) {
  for (var x = 0; x < row; x++) {
    triangle += "#";
  }
  triangle += "\n";
}

console.log(triangle);

You can also try this solution with String#repeat

var triangle = '';

for (var row = 1; row <= 7; row++) {
  triangle += '#'.repeat(row) + '\n';
}

console.log(triangle);

function triangle(num) {
for(let i = '#'; i.length < num; i+='#') {
    console.log(i)
 }
}

Try code below:

function generatePyramid() {
    var totalNumberofRows = 7;
    var output="";
    for (var i = 1; i <= totalNumberofRows; i++) {
        for (var j = 1; j <= i; j++) {
            output+= "#  ";
        }
        print(output);
        output="";
    }
}  
generatePyramid();

How it works: http://rextester./ULY85622

EDIT: Fixed it by just adding one line - you need to reinitialize the variable "line" after each row iteration

var line, triangle;
line = triangle = "";

for (var row = 1; row <= 7; row++) {
  line="";
  for (var x = 0; x < row; x++) {
    line += "#";
  }
  triangle += line + "\n";
}

console.log(triangle);

You need to reset line after each loop because it is accumulating all # on each cycle:

var line, triangle;
line = triangle = "";

for (var row = 1; row <= 7; row++) {
  line = "" // Add this line
  for (var x = 0; x < row; x++) {
    line += "#";
  }
  triangle += line + "\n";
}

console.log(triangle);

Your code is incorrect because line follows the following steps on each cycle:

  • row 1: line starts as '' and ends as # (Adds #)
  • row 2: line starts as # and ends as ### (Adds ##)
  • row 3: line starts as ### and ends as ###### (Adds ###)
  • row 4: line starts as ###### and ends as ########## (Adds ####)
  • row 5: line starts as ########## and ends as ############### (Adds #####)
  • row 6: line starts as ############### and ends as ##################### (Adds ######)
  • row 7: line starts as ##################### and ends as ############################ (Adds #######)
发布评论

评论列表(0)

  1. 暂无评论