6. We made a friend: May someone correct my code?


#1



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

Can someone correct this code and explain the actions of the two functions?

Error message:
Oops, try again. It looks like you didn't add an person object to the end of your contacts array.

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, contacts[contacts.length]];

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 - 1; i++) {
       if (contacts[i].lastName === lastName) {
           printPerson(contacts[i]);
       }
    }
};
search("Jones");

function add(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        phoneNumber: phoneNumber,
        email: email
    }
};
    
    add("Joe", "Barnes", "Joe.Barnes@example.com", "32456456");
    list()


#2

@kenneth1026

Remove in this line

this: