-1

If I have variables and a for loop with a condition set-up like this:

var scores = [23, 53, 85];
var arrayLength = scores.length;
var i;

for(i = 0; i < arrayLength; i++)

Does the i refer to the scores array indexed position of 0, or is i just the counter number, which is set to 0?

I'm kinda confused on understanding what's happening.

Any help is appreciated!

2
  • 2
    i is just a number variable which is increment on each iteration up to the maximum index of the array. Commented Aug 1, 2016 at 15:28
  • As Pranav said it's just a number variable but you can use it to reference the indexes of the array like scores[i] Commented Aug 1, 2016 at 15:30

5 Answers 5

1

Here you can see it in action:

var scores = [23, 53, 85];
var arrayLength = scores.length;
var i;

for(i = 0; i < arrayLength; i++) {
  console.log('i = ' + i + ', scores[i] = ' + scores[i]);
}
console.log('End of for loop: i = ' + i);

One important thing to understand is that i will be incremented until the condition i < arrayLength is not met anymore. So it will reach the value 3 but the for loop will end immediately. Therefore, the code inside the loop is not executed for i = 3.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, so if my var inside the for loop was set to i = 1, and then I ran scores[i];, the first iteration would output 53?
Thanks for clearing that up and showing me the snippet in action. Wish everybody would do that; it was very helpful!
0

i is just a counter number which is initially set to 0 and increments up to arrayLength (3 in this case)

Comments

0

i just refers to a number that (in this case) counts up from 0 until arrayLength. You have to explicitly access the values in the array at each i by using scores[i], after which you can modify/use the value in any way you see fit.

1 Comment

This was a clear explanation for me to understand what's going on!
0

i is the counter number.

A for loop works like so:

For each value of array length, use i as a counter variable

each time through the loop increment the variable of i when you are done (i++)

you could expand it like so...

for(i = 0; i < arrayLength; i++)
{
  console.log('position ' + i + ' is ' + scores[i]);
}//now that I am done, increment i and go through again until i is no longer less than array length

Comments

0

Right so you have the set the i as a variable by initially going

var i;

Within the for statement you have set the i variable to 0.

for(i = 0; i < arrayLength; i++){
}

Then the for statement is saying if i is less than the array length run the for statement. Each time the for statement runs you are adding 1 to i because of i++;

Every time the for statement will check to see if i is less than the arrayLength if it isnt it will exit out of the for.

So for this instance the for each would run 3 times because the array length is 3.

Make sure you have open and closing brackets on the for statement

So your for statement should look like this. for(i = 0; i < arrayLength; i++){ }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.