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 trialyuds ds
1,169 PointsWhy 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
231,269 PointsThe 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.
yuds ds
1,169 PointsThank you very much
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 PointsSTill 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
231,269 PointsThat's right, but "x" was already 5 when the function was called. It is "i" that gets incremented.
rhupp
11,019 PointsDoes 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
231,269 PointsSounds like you got it!
May I ask where the name "lockwiregirl" comes from?
rhupp
11,019 PointsOf 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
231,269 PointsThat 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!
rhupp
11,019 PointsThank 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.
Miguel Barra
6,855 PointsI 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.
Chris DiPiero
12,266 PointsI 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:
- i = 0
- i = 3
- log i + 1 // 4
- i++
- GOTO 2 (anybody ever program in BASIC?)
obviously, that's not happening. What appears to be happening is this...
- i=0
- log i + 1 // 1 on first loop, 5 on all others
- i = 3
- i++ // i ===4
- GOTO 2
Why though, if the assignment is BEFORE the log?
Steven Parker
231,269 PointsIt'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.
Chris DiPiero
12,266 PointsSo 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
231,269 PointsNot 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!
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsBruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsWe need to see your code to help :)