Reputation: 1170
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:
Upvotes: 1
Views: 4024
Reputation: 21
function triangle(num) {
for(let i = '#'; i.length < num; i+='#') {
console.log(i)
}
}
Upvotes: 2
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:
line
starts as '' and ends as #
(Adds #
)line
starts as #
and ends as ###
(Adds ##
)line
starts as ###
and ends as ######
(Adds ###
)line
starts as ######
and ends as ##########
(Adds ####
)line
starts as ##########
and ends as ###############
(Adds #####
)line
starts as ###############
and ends as #####################
(Adds ######
)line
starts as #####################
and ends as ############################
(Adds #######
)Upvotes: 1
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
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
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
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