6. We Made a Friend!


#1


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


Hello. I am checking other peoples solution to this last exercise and it seems that everyone did it differently then me. This solution was accepted but i just want to know if my way should be used or should i follow the "mainstream" solution with:

contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};

and why?
Below is how i did it.

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

function add(firstName, lastName, phoneNumber, email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.phoneNumber = phoneNumber;
    this.email = email;
    
    contacts[contacts.length] = this;
    };
    
add ("Marko", "Polo", "696-696", "marko.polo@roadtochina.com");
list();


#2

Can we see your solution, so we can understand what you might've been missing?


#3

I posted my entire solution in my first post but the part that is different from others is this:

function add(firstName, lastName, phoneNumber, email) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.email = email;

contacts[contacts.length] = this;
};

and in several examples i have seen people do it this way:

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


#4

Hi! I actually solved just like you as well, with the "this"!

But I think the other solution follows their example:

contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};

Maybe that's why people used too =)


#5

So basically it is the same thing just done in a bit different way. Thanks.


#6

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