6. we made a friend


#1

this is my code.
what's wrong..??

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( var j = 0; j < contactsLength;j++) {

    if ( contacts[j].lastName === lastName ) {
        printPerson(contacts[j] );

    }

}

};

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

};
add("mahmud","ph","mahmudie@gmail.com",284230);
search("Jones");


#2

i have a message

Oops, try again. Make sure the contacts array still exists. Your code threw the following error when checking contacts: TypeError: Cannot read property 'length' of undefined