Building address book


#1

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"*/
var search = function (lastName){
    var contactsLength = contacts.length;
    for(i=0; i<2; i++){
        if(contacts[i].lastName==lastName){
            printPerson(contacts[i]);
        }
    };
};

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

add("jones","bones","rbrrr@example.com","5555555555");

list();

Can someone tell me where I am going wrong? When I log into the console it is saying the problem is with --->this.firstName:firstName, inside the add function.

Thank you!


#2

why did you use this? i don't see a reason, when you created bob:

firstName: "Bob",

you didn't use this either, why is this any different?


#3

that was it... Thank you!


#4