Listing Everybody

like this : var contactsLength = contacts.length

Yes, exactly. Now you have stored the number of elements in that array in your contactsLength variable. So no matter if there are 2 or 50, that number can be used in the next step of looping through each item as you print them.

OK, so i did that and it says Oops, try again. It looks like your list function didn’t console.log “Bob Jones”. Did you remember to call list(); after creating it?

Could you show me?

New Code

That will show me my own version of the exercise. You’ll have to copy/paste you code.

I just need the whole list function.

How to post code in this forum.

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 < 2; i++) console.log(printPerson); list }

If you use the three backtick method, it looks nicer - IMO. Let’s focus on the list function.

function list() {
var contactsLength = contacts.length
for (var i = 0; i < 2; i++) 
    console.log(printPerson);
    list
}

So, now, before we get too far, can you see where you could use your contactsLength variable in your for loop?

console.log(contacts.length)

What if I asked you why you put a 2 in there?

Take out the two and put the contacts.length

How about using your new variable instead?

so put contactsLength in instead of contacts.length

Yes, that way, if you had 3 zillion contacts it would not have to recalculate the length each time you loop.

Show me what your list function looks like with contactsLength in there.

Like this
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(printPerson); list }

That is starting to look great!

Now, on that last line, take that list out of there, you don’t want to get into a situation where you are calling your function from inside your function. There are reasons to do that but you need a way to exit – we don’t want to worry about that today.

Once you have that then we are going to work on how to get your contacts to print.

Like this
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(printPerson); }

Alright, now let’s look at that line.

You don’t need console.log() because your printPerson function is going to do that part for you – do you see how that is going to work?

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++); }