4. Listing Everybody


#1

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

Hello I got “TypeError: Cannot read property ‘firstName’ of undefined” but I believe it should be ok. Can you please look at it and check where I made mistake?

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

#2

you have an unnecessary semi-colon after i++) which interrupts the loop, causing problems.


#3

OMG, thanks man :slight_smile:

Any advices how to avoid such mistakes? :slight_smile:


#4

you’re welcome :slight_smile:

I wish i knew, i once made this terrible mistake:

do {
    // don't remember what loop did
} while (someVariable = 1)

and i couldn’t figure it out what the mistake was, why i had an infinity loop

as you code more, your debug skills will get better, and you will make mistake less easily, so i think the only advise here is to keep coding/programming to get better

if you can’t see what the mistake in my code was, i used a single equal sign (which means assign) rather then two or three equal signs, which means check if equal, took me 30-60 minutes to find it :frowning:


#5

Pity to hear that there is no other way to avoid such mistakes then to be more skilled in writing the code :slight_smile:

And this mistake in your code may took me far more then 30-60min to find this out :slight_smile:

Hard life coders have it seems :smiley:


#6

yep, programmers life is very difficult, if you have some time to spare:

https://www.quora.com/Whats-the-hardest-bug-youve-debugged

some of these bugs are insane :frowning:


#7

This type of bug mentioned by the Dave Baggett should be considered as pretty one :slight_smile:


#8

good luck finding such a bug, as you can read, that takes weeks :open_mouth:


#9

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