12/13 help


#1

I don't understand the work of i in this piece of code.


#2

Please present the code you are using....??


#3

var names = ["Aphrodite", "Sophocles", "John", "Catherine", "Paraskevi"];

for (var i = 0; i < 4; i ++) {
console.log("I know someone called" + " " names[3]);
}


#4

Well you have an array with 5 entries from which you can get the values by arrayName[index] where index is an increasing number from 0 to array.length. So for the current example to get the 3. name you'd use:

names[2]

which therefor have a value of "John".
Now as you for loop counts through the numbers from 0 to array.length this enables you to have a look at each array value by using the counting variable i as index. Currently your using 3 which gives you "Catherine" 5 times but if you use i it would give you another name each loop. Which makes for loops perfect to go through arrays. You can even improve it by using names.length instead of 4 because this always works no matter how long your array is.