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

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

list();

What is wrong with my code the error said:

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


#2

So your problem is here. Note what you equaled i to in your variable. In contactsLength, it doesn't start at 1 but at 0. We know this from our instructions in step 3.) create a for loop that cycles from 0 up to one less than the number of items in the contacts array. Therefore, what do we need to change in order for this to happen in your code?


#3

oh yes

thanks a lot!
I had meant i: my tipping mistake, iI check it again and again but i don' found it

Now it works. Great


#4

No problem! :smiley: And don't worry about it, typos and such are very easy mistakes. You are bound to have a few happen here or there. That is why the forum is around! To give you a fresh set of eyes to look at your code when your stuck. :thumbsup:


#5

This line of code is incorrect:

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

you started out using the letter i, but then use the number 1 for this line 1<contactsLength should be i<contactsLength;

These subtle errors are hard to find in the code, and very frustrating. Hope this helps.

Mary Ann


#6

Thanks maryanndixon28
Yes, bibleman13 said me the same.

It was my tipping mistake :wink: Now it works

Thanks for your help


#7

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