We Made a Friend


#1


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


It says that the contact does not have the properties


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

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

search ("Jones");

add("Jhon", "Lane", "JhonLan@email.com", "2233445566");


#2

are you sure you want to use the new Object()? This makes for a relative difficult solution.

i would use the literal notation:

test = {
    "property1": "value1",
    "property2": "value2",
}

that makes it a lot easier.

but if you really want to achieve this exercise with new Object(), you could do:

contacts[contacts.length] = new Object();
new_contact = contacts.length -1 // get new contact we are creating
new_contact.firstName = firstName // give new contact a first name property
// with a value of firstName (function parameter)

i am not sure what you are doing with this keyword, we need to create the properties to contact object we are creating


#3

First here:

if (lastName = contacts[i].lastName)

You used assign operator = but need comparison ===.
Next error is here:

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

You can't use keyword this here because this was not defined by any object and now this is undefined. Instead of this use contacts:

function add(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = new Object(); // at the end of contacts object we add new object
    contacts.firstName = firstName; // initialize firstName 
    contacts.lastName = lastName; // initialize lastName 
    contacts.email = email; // initialize email
    contacts.phoneNumber = phoneNumber; // initialize phoneNumber
};

#4

this doesn't work? contacts is an array

you add a new object to array, but then you would first need to retrieve object from array:

new_contact = contacts.length -1

then you can add firstName property to your new object:

new_contact.firstName = firstName

hm... your answer inspired me.

@alex00139, you might want to first create the object, add the properties to object, then add the object to array, if you want to stay with new Object()


#5

Shame of me! :confused: Of course it doesn't work.

I tried to find solution for constructor notation but forgot to retrieve object from array. hmm... how did I test my solution?!!

I agree that in this case literal notation is easier for understanding and is better solution.


#6

well, i later realized it would be ideal to first create the object:

new_contact = new Object();

then add the properties:

new_contact.firstName = firstName

then add to array:

contacts[contacts.length] = new_contact

this solution isn't much worse then using the literal notation.


#7

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