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 trialfredyrosales
10,572 PointsNeed a brief overview.
Guys i know we went over this a while back but i can't quit understand this line of code anymore. It would be much appreciated if someone can explain it for me, Thank You! for (var i = 0; i < students.length; i += 1) { student = students[i];
2 Answers
Matt Milburn
20,786 PointsHi Fredy,
So let's break this down...
// Here we have an array (or list) of 4 student names
var students = ['Matt', 'John', 'Mary', 'Jane'];
// Now we are going to "loop" through the `students` array...
// 1. var i = 0 // create a counter variable called `i` (aka "iteration")
// 2. i < students.length // run this loop as long as this condition is true
// 3. i += 1 // increment `i` at the end of every loop iteration
for (var i = 0; i < students.length; i += 1) {
// Use "bracket notation" to access the student at the array index of `i` (aka "position")
var student = students[i];
// Let's also print this to the page with a line break after `<br>` each student
document.write(i + ': student + '<br>');
}
This will output the following HTML to the page...
0: Matt<br>
1: John<br>
2: Mary<br>
3: Jane<br>
Vance Rivera
18,322 PointsThe for loop loops through a block of code for a specified number of times.
//var i = 0
//var i : is the variable that will be your index variable for students starting at 0 which is the first student
//i < students.length
//condition that will determine how many times to run the block of code inside the loop
//while var i is less then students array length run this code block
//i += 1
//each time the code block is executed increment var i by 1
//this allows us to target the next student in the students array and execute the code in the loop per student
for(var i = 0; i < students.length; i += 1){
var student = students[i];
///code to run for each loop with new student
}
hope this helps. Cheers!
Daniel Springer
7,600 PointsDaniel Springer
7,600 Pointshey it seems that what your dealing with there is whats called a for loop. Let me try to explain it out in plain english lol.
so you have a variable in your loop (i) that youve assigned a value of zero. you have told js that for each time that i is less than students.length (the number of objects in your array) run the code in the brackets {} and than when you are done running that code increment the variable i by 1.
so if you had 8 people in your students array. the code would run 8 times. stopping the moment that i is no longer smaller than the students.length.
Hope the explanation helps ^_^