Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing ES2015 ES2015 Basics Let and Const

Why the program print loop is 5 instead of 4 infinitely when i = 3?

'use strict';

(function initLoop() { function doLoop(x) { i = 3; console.log('loop:', x); }

for (var i = 0; i < 10; i++) { doLoop(i + 1); } })();

4 Answers

Steven Parker
Steven Parker
231,236 Points

The function changes the global variable "i" to 3, and then when it returns the loop increments it to 4 ("i++"), and then when the function is called again, 1 is added to make 5 ("doLoop(i + 1)"). Then inside the function, the print uses the passed-in argument instead of "i", so it prints 5 each time.

This is a great example of why global variables are often a bad idea.

Thank you very much

Akash Sharma
seal-mask
.a{fill-rule:evenodd;}techdegree
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 Points

STill does not make sense to me,what do you mean that "when it returns the loop increments it to 4" like doesn;t console.log ('loop': x) have to be executed before it can return to the loop?

Steven Parker
Steven Parker
231,236 Points

That's right, but "x" was already 5 when the function was called. It is "i" that gets incremented.

Does this mean that at the top of the infinite loop's output, the very first line would read loop: 1?

If so, I think I get it. i gets sent to doLoop as 0, which means that x arrives as 1 and is logged to the console with 'loop: '. Meanwhile, i is reassigned to 3 and incremented to 4 when the loop completes. Since i is 4, i + 1 is now 5, which is what gets logged to the console with 'loop: ' the second time around, and because i keeps getting reassigned to 3, the loop continues.

Okay. Now it makes sense. :)

Steven Parker
Steven Parker
231,236 Points

Sounds like you got it!

May I ask where the name "lockwiregirl" comes from?

Of course! I trained as an aircraft mechanic and was bullied out of the industry. One of the things I worked with most often was safety wire, and 'lockwire' is another name for that. It's tough, and can be a pain to work with if you don't know how to handle it, but it's indispensable. I suppose I like the symbolism. :)

Steven Parker
Steven Parker
231,236 Points

That would've been my guess, and sorry to hear about your experience. But I'm glad it didn't ruin the concept for you because I agree the symbolism is cool. And hopefully you'll find software development more enjoyable and profitable!

Thank you, Steven! It's been great so far, and I look forward to learning much more, thanks to professionals like you who pay it forward.

I think I know what's going on. First, you need to know how for loop works.

for (var i = 0; i < 10; i++) {
  doLoop(i+1);
}

We have the "i" variable is equal to 0, then we check if 0 is less than ten which is true, so we enter the loop. Then, we call the function inside the loop. Here, because of scope, the "i" variable which is 3 (because inside the function we assign 3 to the "i" variable) is increased by one (i++) therefore is 4 and plus 1 is 5. I hope my answer will be useful.

I don't understand why 3 is being assigned to i AFTER the console.log executes. My assumption, based on how the code is structured, is this:

  1. i = 0
  2. i = 3
  3. log i + 1 // 4
  4. i++
  5. GOTO 2 (anybody ever program in BASIC?)

obviously, that's not happening. What appears to be happening is this...

  1. i=0
  2. log i + 1 // 1 on first loop, 5 on all others
  3. i = 3
  4. i++ // i ===4
  5. GOTO 2

Why though, if the assignment is BEFORE the log?

Steven Parker
Steven Parker
231,236 Points

It's not "i" that gets logged. It's "x", which is one more than what "i" used to be (before being set to 3). So the assignment of "i" does not affect what gets logged, but it does affect the value it will have in the loop, and what will be logged next time.

So the FIRST thing the called function does is assign/calculate x (0 + 1), THEN assign 3 to i, THEN log x (which was ALREADY calculated as 0 + 1), then i++ (3 + 1), then assign calculate x (4 + 1) ad infinitum...

Steven, you're a rockstar! Thanks!

Steven Parker
Steven Parker
231,236 Points

Not quite. The inner function ("doLoop") does not do the calculation. That is done before it is called..

The first thing done by the called function is assign 3 to i. But the rest of your description is correct.
Happy coding!