4 help


#1

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list ()
{var contactsLength = contacts.length;

for (var i=0; i < contactsLength; i++); {printPerson(contacts[i])};
}
list();

Not sure what is wrong? Keep getting this error

TypeError: Cannot read property 'firstName' of undefined

thanks.


#2

Hi!
You've a semicolon just after the declaration of your for loop. Erase it!


#3

Why is

contactsLength = contacts.length inside the list function needed? I tested it without that and it worked as well. So my code Looks like that and it works as well. Can anyone give me the Explanation please?

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list() {
for (var i = 0; i < contacts.length; i++) {
printPerson(contacts[i]);
}
}

list();

thanks!


#4

Your function list has more one ;
It should be this:

function list ()
{var contactsLength = contacts.length;

for (var i=0; i < contactsLength; i++) {printPerson(contacts[i])};
}
list();