4. Listing Everybody


#1

I appear to be stuck on this code..I thought my code was fine, but I am getting the error of "Oops, try again.
It looks like your list function didn't console.log "Bob Jones". Did you remember to call list(); after creating it?"
My code is below:

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++) {
        console.log(contacts[i]);
    };
};

list(contacts);

Can anyone help please?


#2

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

what is contacts?isn't it an array?
so here contacts[i] means contacts[0] so what should it print?

why dont you call the printPerson function inside your list function?

list(contacts);

when should we pass an argument?Though there is no need to pass an argument here.


#3

Yeah so contacts is the array I've created and I've tried posting it with the "printPerson" function inside the list and it gives me the same error message. I've also tried list with no argument passed through it and it still doesn't work.
I'm just not sure where I'm going wrong?


#4

can you please post update version of your code?


#5

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++) {
console.log(contactsLength[i]);
};
};

list();

So i've now tried declaring contactsLength (as it suggests i have to in the instructions) and it still doesnt work? Same message

Oops, try again.
It looks like your list function didn't console.log "Bob Jones". Did you remember to call list(); after creating it?

and also now I have 4 undefined messages in the console log preview.


#6

Fixed it now! Correct code was:
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 (i = 0; i < contacts.length; i++) {
printPerson(contacts[i]);
};
};

list();

Must be my reading thats letting me down! Thanks for the help though


#7

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