We made a friend - need help


#1

Dear all,
I have a question regarding the last excercise in this lesson.
`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]);
}
}
}

search("Jones");
function add(firstName, lastName, email, phoneNumber){
contacts[contacts.length] = new Object();
contacts[contacts.length].firstName = firstName;
contacts[contacts.length].lastName = lastName;
contacts[contacts.length].email = email;
contacts[contacts.length].phoneNumber = phoneNumber;
}

add("Julia", "Maier", "Julia@example.com", "4711");

list();`

What's wrong?
It's telling me: "TypeError: contacts[contacts.length] is undefined"

Thank you! :slight_smile:


#2

This post was flagged by the community and is temporarily hidden.


#4

Thank you for the advice - now it is working finally :slight_smile: