6. We made a friend - List error


#1


Hi everyone, I am stuck right at the end of this exercise. It seems I have added my new contact correctly, but it won't call the list function correctly. Am I missing something?

Thanks.


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


Oops, try again.
It looks like you didn't log "Jim Morrison" to the console. Did you remember to call list() after adding your new contact?


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

/*Create a search function
then call it passing "Jones"*/
function search (lastName) {
    var contactsLength = contacts.length;
    for (var j = 0; j < contactsLength; j++) {
        if (lastName === contacts[j].lastName) {
            printPerson(contacts[j]);
        }
    }
}
search ("Jones");

function add (firstName, lastName, email, phoneNumber) {
    var contactsLength = contacts.length;
    contacts [contactsLength] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    };
    array[contactsLength] = contacts[contactsLength];

}

add ("Jim", "Morrison", "jim@morrison.net", "555 123-4444");

list ();


#2

Function add() has strange line. I think you need to remove it.


#3

doh!! staring me right in the face. Thanks. working perfectly.


#4

Hi, there is another way to shorten the code above.

for (i=0; i<contacts.length; i++)

and it would make the code more efficient :wink:


#5

My interpretation was that we create a function to to create objects. This function would add new objects when we call the add function. I don't think you need the array[contactsLength] = contacts[contactsLength] since the function already added the objects to the array. If you want to be sure that the function is doing this put the console.log(contacts); at the bottom of your txt editor this should print out your array with your added object. If you still are not sure call add another time with new parameters and keep it before the console.log(); or it will not print the newly added object.

function add(firstName,lastName,email,phoneNumber){
    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    };
}

add("Ryan", "Marshall", "620-257-1633", "Code@gmail.com");
list();
search('Jones');
console.log(contacts);

#6

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