4: Listing Everybody


#1



https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661#


TypeError: Cannot read property 'firstName' of undefined.

And...

Oops, try again. Your code threw the following error when trying to call list(): TypeError: Cannot read property 'firstName' of undefined.


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


#2

var list = function() {
    var contactsLength = contacts.length; 
    for(i=0; i<contacts.length;i++); // LINE
    printPerson(contacts[i]);
}

notice the semicolon at the end of "LINE"? Do you know what it implies? :wink:

Hint: It ends the loop there and hence the line printPerson(contacts[i]); is interpreted to be outside the loop. it's best to use curly braces {} which usually avoid these errors.


#3

Thank you, kind sir!

But, now it is only printing "Bob Jones" twice (your code didn't cause that).
What should I add to make it print "Mary Johnson" as well?

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


#4

Glad to know you made progress :slight_smile:

In the code:

var list = function() {
    var contactsLength = contacts.length; 
    for(i=0; i<contacts.length;i++); // LINE
    printPerson(contacts[i]);
}

we now know what problem that semicolon is causing. It is throwing printPerson(contacts[i]); out of the loop when we want it inside the loop. Hence, we need to remove the semicolon. Now our code becomes:

var list = function() {
    var contactsLength = contacts.length; 
    for(i=0; i<contacts.length;i++) // LINE
    printPerson(contacts[i]);
}

After adding var and formatting it a bit:

var list = function() {
    var contactsLength = contacts.length; 

    for(var i = 0;  i < contacts.length; i++) // LINE
        printPerson(contacts[i]);
}

It should work as expected - print both bob and mary.


#5

Thank you once more! Code works perfectly.
I'll try to format my code like your in the future - much neater!


#6

Hi Correct me if im wrong but

"var contactsLength = contact.length;" isn't actually doing anything


#7

Yes, you are right. We need to have the code look like:

for(var i = 0;  i < contactsLength; i++)

for it to have some effect.


#8

You could've also used your newly created variable inside your for loop. Also, the semi-colon is missing on your function call, but your code does run in the code.academy's compiler.
Here is how I wrote it:

function list()
{
var contactsLength = contacts.length;
for(var i=0;i< contactsLength; i++)
{
printPerson(contacts[i]);
}
}
list();