4.Listing Everybody


#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 (contacts=0; contacts<contactsLength; contacts++);
    console.log(printPerson(contacts));
};

list();

It says I passed the exercise but it's obviously wrong since the console logs pleanty of "udefined". What is going on?


#2

first thing is here, you need to use a { instead of a semicolon because that is proper syntax, it should look like

for(contacts = 0; contacts < contactsLength; contacts++) {

}

also, inside of the for loop, you need use the line

printPerson(contacts[contact])

you don't need to console.log printPerson because that function already has a console.log line, and you need to use contacts[contact] because contacts is an array that you are iterating through.


#3

Thanks! but it still won't work because I haven't define i anywhere actually :slight_smile: so I will try to do it this way, with the i variable, not using contacts variable


#4

You're right sorry, it should be contacts[contact], I just use i in my code


#5

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