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 trialCurtis Vanzandt
9,165 PointsHow does this loop work if the variable entitled "i" hasn't been called anywhere in the loop other than in the start?
I understand loops, basically, but I am confused. When you declared the variable "i", you set the statement up well. But after that, "i" wasn't used. I don't understand how the computer knew to loop that 10 times if "i" wasn't used at any point inside the loop.
3 Answers
Jordan Hewett
9,437 PointsThe loop runs until it meets a condition that you give it. So for the example he gives, it keeps repeating the code inside the curly braces until "i" = 10.
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 Points'i' is an iterator. The computer keeps track of it to track its progress through the loop. Once the middle condition is met, the code will cease. If you need to, you can use 'i' in your code, but it's not essential.
for (let i = 0; i < 10; i++) {
//your code
const myArray = [];
myArray.push(i);
}
//myArray == [1,2,3,4,5,6,7...10]
Tom Couillard
19,866 PointsIn a loop let i =0 is used as an iterator, you can choose to use this within the loop to say go through an array ie for (let i = 0; i = array.length; i++){do something to array[i]} or you can just use it for something like say you want to simply run a function like an animation 10 times. Hope this helps make it easier to understand.
kim corson
Full Stack JavaScript Techdegree Student 1,783 PointsSo i does not have to directly connect to the code in the braces to configure a certain aspect of that code to run as the iterator? I think why I am confused is because, here is a for loop using a variable i. Variables in code in the past have always connected directly by name or indirectly by calling a function with a variable in it, where as now... the variable i is just declared. Like var i = 1; Okay, I mean declared and defined as a number. What relation does the variable i being 1, have any connection to red = Math.floor( Math.random( ) * 256 ); That is a statement of getting any number between 0 and 255. So how does it take all of these colors together, and randomize them the amount of times in the condition. I am sorry I feel so dumb coding sometimes :( I kind of understand that the connection is based on the braces but how does the code in the braces know how to react to the for loop? I am uncertain of a better explanation but I am confused lol.