baconLikeTheKevin725
baconLikeTheKevin725

Reputation: 245

Can someone explain why this this for loop prints out this specific pattern?

I thought I had a good understanding of for loops, but now that I have started to try looping patterns with for loops, things have gotten a lot more confusing. For example, I know that the following code will print out:

for (var line = “#”; line.length < 8; line += “#”) console.log(line);

Output will be:

#
##
###
####
#####
######
#######

This is what I understand so far about this loop:

  1. Firstly, the loop is initialized by creating a variable “line” to store the value “#” which is only one character.
  2. Next, “line.length<8” checks that the length of the string stored in the variable “line” is less than 8 characters long.
  3. The third part “line += “#”” updates the value stored in the line variable by adding “#” for each iteration that line.length<8 is true.
  4. The length of the string (and therefore line.length) is being updated for each iteration of the loop because a “#” value is being added each time.

Can someone explain to me why # is being added once, then twice, then three times etc. From the (incorrect) understanding that I have, I keep thinking that it’ll look like:

#1
#2
#3

etc.

Upvotes: 0

Views: 49

Answers (3)

T. B.
T. B.

Reputation: 174

In the for loop, there are three statements, separated by semicolons.

The first statement is an initialization, as you correctly identified.

The second statement is a condition. This is checked before every iteration, and will execute the loop code (in this case, console.log) if the condition is true.

The last statement is executed after every iteration.

In your loop, line is initialized to "#". Then, it checks if line.length < 8. Since it is, console.log is called. Then, line += "#" is executed, which adds "#" to the end of line. At this point, line is "##". It will then check if line.length < 8, and so on, until the condition isn't true.

This article might help your understanding.

Upvotes: 0

Justin Herter
Justin Herter

Reputation: 590

the var Line persists throughout the loop so if you had ## and again the loop adds # you end up with ###. In this case you are calling .length on a string which gives you the length of the string in individual characters.

so essentially what you are asking the loop to do is append a # to line until there are 8 of them and stop.

Hope this helps

Upvotes: 0

Doug
Doug

Reputation: 140

It seems like you explained it well yourself.

3.The third part “line += “#”” updates the value stored in the line variable > by adding “#” for each iteration that line.length<8 is true.

Each time through the loop one additional # is being concatenated to the end of the string. It starts as #, then ##, then ###.

When we call

console.log(line);

it just prints the string itself, it has nothing to do with the length of the string, or any line number.

At the start of each pass, a check is made on the length of the string. In this case the length is simply how many # are in the string.

Upvotes: 1

Related Questions