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[2]<contacts.length;contacts++){
            
    }
}

This code is dont working for me!
Please help me...


#2

@hillelblender,
Please read the article
http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript
to get an idear
how to loop through an Array....


#3

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}
list();
this works but i am not sure about the contactsLength = contacts.length why we need this part


#4

I am having trouble too, it sucks I am "missing variable" name

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;
    var (for i=0; i< contacts.length; i++){
        printPerson[i]
    };
}
list();

#5

where you call the printPerson your code should be
printPerson(contacts[i]);


#6

Check the line of text where your "for" loop is, it's all wacky. You've got the parenthesis in the wrong spot AND you need to remove the "var" from before "for".