We made a friend - code issue, help!


#1

All,

I am stuck at the last step of "We made a friend". Can you please help me identify what is wrong with below code?
Thanks in advance!!

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,contact];

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]);
}
}
};

search("Jones");
function add(firstName,lastName,email,phoneNumber) {
var contact = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
};
list();


#2

What are we looking for, in what way does it act differently from what you want?


#3
  1. var contacts = [bob, mary,contact];
    You're adding a contact object taht doesn't exist to the array contacts.

  2. function add(firstName, lastName, email, phoneNumber) {
    var contact = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
    };
    };

You are creating a new contact object, instead you should just add a new object to the exiting contacts array position when calling the add function.