Moaaz Bhnas
Moaaz Bhnas

Reputation: 1170

Building triangle with nested loops

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:
enter image description here

But this is the result I got:
enter image description here

Upvotes: 1

Views: 4024

Answers (6)

Akaki Khotcholava
Akaki Khotcholava

Reputation: 21

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

Upvotes: 2

Giovanni Benussi
Giovanni Benussi

Reputation: 3500

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 #######)

Upvotes: 1

ionizer
ionizer

Reputation: 1721

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);

Upvotes: 1

epascarello
epascarello

Reputation: 207501

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);

Upvotes: 2

Suren Srapyan
Suren Srapyan

Reputation: 68645

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);

Upvotes: 2

Vyacheslav
Vyacheslav

Reputation: 156

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.com/ULY85622

Upvotes: 1

Related Questions