Hello, I’m stuck somewhere in the for loops. I hope someone could help me. I came across this problem when I did the looping triangle exercise.I thought I got it correctly, but it was executed differently on the lecture. So I got curious as to how the code blocks are executed and I got stuck here.
I wanted to understand how does the updating of my toPrint variable works here. I have 2 scenarios, one is I’ve declared the var toPrint before the loop call, and the other one is inside the code block of the first loop.
They both return different set of values so I’m trying to understand how the first example works.
First example returns;
var toPrint = “#”;
var num_rows = 7;
for(var row = 0;row<num_rows; row++){
for(var column = 0;column<row; column++){toPrint = toPrint + "#"};
console.log(toPrint)
}
Second example returns;
var num_rows = 7;
for(var row = 0;row<num_rows; row++){var toPrint = “#”;
for(var column = 0;column<row; column++){toPrint = toPrint + "#"};
console.log(toPrint)
}
What only make sense to me is the second example. I’ve been playing around and searching thru the net to understand why they return different values and got nothing. Where does the increments of +1,+2,+3,+4,+5,+6 of "#"s from the first example pulls? Please help me understand.
Thank you!