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
for (var i = 0; i < 2; i++) {
    console.log(printPerson);

SyntaxError: Unexpected token var
My Code


#2

You are getting that error because functions need { }, like this:

function list() {
   code in here;
}

Once you sort that out, there are other issues.


#3

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

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


#4

What is your plan with this line of code?

var contactsLength = contacts

#5

I don't know, i am following what it says


#6

Could you post a link to the exercise that you are working on please?


#7

Sure
Listing Everybody


#8

Thanks.

For that bit that we are talking about

var contactsLength = contacts

this is the relevant instruction:

define a variable to store the number of items in the contacts array. Call it contactsLength.

You've got the:

var contactsLength =

right, but if you make it equal to contacts there is nothing there that is getting the length for you. Making it equal to contacts is just copying the whole contacts array.

Do you remember how to get the length of something with JavaScript?


#9

Yes, i remember
string.length


#10

Right, so you can that same thing with the contacts array to get the length of it, and in this case, length will mean the number of items in it.


#11

like this : var contactsLength = contacts.length


#12

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.


#13

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?


#14

Could you show me?


#15

New Code


#16

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.


#17

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
}


#18

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?


#19

console.log(contacts.length)


#20

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