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

Confusion over: for (var i = 0; i < something.length; i += 1) {

This line of code keeps causing confusion for me throughout my JavaScript journey. I don't know why it won't clarify in my brain like the other aspects.

Pretty much every challenge requires this line of code (slightly different for each challenge):

for (var i = 0; i < students.length; i += 1) {

I understand that it is a counter written to loop a specific number of times, but when I come to applying that logic to a problem, I get lost.

For example The Student Record Search challenge.

var message = '';
var student;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

for (var i = 0; i < students.length; i += 1) {
  student = students[i];
  message += '<h2>Student: ' + student.name + '</h2>';
  message += '<p>Track: ' + student.track + '</p>';
  message += '<p>Points: ' + student.points + '</p>';
  message += '<p>Achievements: ' + student.achievements + '</p>';
}

I understand that the loop wants to locate each entry and print it to the screen. That makes sense as I want all of the array entries listed, but when I get to the next part of the challenge I'm not sure how it applies. I want the loop to run endlessly until it breaks so why have a loop counter?

Thanks for your help.

6 Answers

Just over-complicating a little bit. Yea the loop i.e. i starts at 0. So:

first pass i = 0 so your student variable is students[0] it now looks at the student with index 0 second pass i = 1 so your student variable is students [1] it now looks at the student with index 1 so on...

The for loop is making it possible to look at every student in your students object. So the for loop has two functions here. It makes sure the for loop ends when its supposed to when i == the length of the students array/object and also to access each individual student.

So Im not exactly sure if this is going to help but a for loop is there to make sure you loop through all of the available data. It ends when the 2nd argument of the for loop is reached.

Without the loop counter your program wouldnt stop and its possible it would start pulling info from places it shouldn't or just crash (endless loop). Now there are loops that you can make run until you manually break it such as a do/while loop.

Hope this helped or at least answered some of your question.

For the second part of the challenge, a while loop is required to bring up a prompt box endlessly until made to break. In this instance, why would the for loop be necessary? That's the bit I'm hung up on.

Ahhh ok sorry about that. So the while loop keeps the program running until you dictate it ends. The for loop is necessary to loop through the array or object so that it prints out or does what you want it to do.

The while loop without the for loop woulnt be able to go through the student and list all the properties of the student. Thats the only reason your using the for loop. Once the for loop is complete then it just starts over at the top of the while loop.

Its just a loop that nested inside of another loop. The while takes care of the overall program and the for loop takes care of the details. Essentially.

So when the user is prompted to enter a student's name to search for their record, the for loop's job is to go through all of the array data until it finds the input match? As opposed to the part 1 of the challenge where the for loop is just required to go through all of the array data and print everything. Is that right?

I guess the counter aspect of it is what confused me. Why the need for it to count up 1 with every loop through. Is it counting each entry in the array as 1? Or rather counting a loop through the entire array as 1?

My thinking: the array length is 5, with every pass of an entry in the array, 1 is added until it reaches the same number as the array length. The counter acts as a position indicator to the loop in relation to the array length. i.e. the counter holds 3 so the loop is at position 4 in the array?

Am I making sense of it or over-complicating?

Thanks

Ok thanks very much. I've been meaning to ask this question for a while as it has been holding back my progress so I appreciate you clearing it up for me.

no problem. I feel i still didn't do a great job really explaining it but in the end if it helped a little im glad.