6. We Made a Friend! - this can't work?


#1

<6. We Made a Friend!> https://www.codecademy.com/courses/building-an-address-book/0/6?curriculum_id=506324b3a7dffd00020bf661#

The correct answer is:

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

How about the one below? It was my initial answer. Is it because of contacts[contacts.length]? That it already specifies it as an element in an array being assigned, so I shouldn't (not don't have to) specify the properties?

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


#2

contacts[contacts.length] means it will append to the array, so you don't need contacts.firstname and so on.

just like the first objects you created, you just created a object (with firstName = firstName and so on) but instead of creating it first and then add it to the contacts array, you do this in one step now


#3

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