Don't know what to do!


#1

Hi all,

I have just ran across a problem in lesson 12. I just don't know how to design the loop! Here is my code:

var names= ["Jeff", "Jim", "Jamie", "Jake", "Daniel"];
for (var i = 0; i > 4; i ++) {
    console.log("I know someone called "+ names[i]);
}
// Don't know what to do with for loop

It would be appreciated if you could tell me what to do,

Thanks so much!


#2

for(var i = 0; i < array name.length; i++) {

Replace array name with what you think should be there, and it will work.


#3

i > 4;

This is the problem because in the first step you declared that var i = 0. This will never be greater than 4, and your loop will stop before it can print anything.

Instead of i > 4;, let's change the condition to i < names.length;. This way our loop will continually scan our array called names, so long as there is content remaining.


#4

Hey there. Thanks a lot for replying. But I really don't know why you would do i = 0, why would you do that?


#5

Because if you console.log(names[0]); you get ''Jeff"

In JS array indexes start with 0 and in this exercise you have to start looping from the first position (that is 0 index)