27. Loop the Loop - Syntax error


#1

So I'm trying do run the code below:


// Our Person constructor
function Person(name, age){
this.name = name;
this.age = age;
};

// Now we can make an array of people

var family = new Array();

family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);


// loop through our new array

for(var i = 0; i <= family.length, i++){
    console.log(family[i].name)
};

But it keeps saying "SyntaxError: Unexpected token )", and I can't see what's wrong...
Can someone help me please?


#2

Never mind, just realized I wasn't isung a semicolon on


for(var i = 0; i <= family.length, i++)


#3

The last index of a list will be 1 less than the length, so don't include the actual length in the loop. The conditional ends with a semi-colon, not a comma.

i < family.length;

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.