We Made a Friend!


#1

I keep getting this error: Oops, try again. It looks like there are still only two items in the contacts array.

My code:

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

function search(lastName) {
    var contactsLength = contacts.length;
    for (var i = 0; i < contacts.length; i ++) {
        if (contacts[i].lastName === lastName) {
            printPerson(contacts[i]);
        } else {
            return "nothing";
        }
    }
}

var add = function(firstName,lastName,email,phoneNumber) {
    contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    email: email,
    phoneNumber: phoneNumber
}
    add("Roni", "Cristmas", "something@gmail.com", "911");
}
list();

6. It says something is wrong with my syntax and unexpected identifier. Not sure what to do. Can post the rest of code if needed for this section
#2

You are calling the add() method while still inside its definition. That's invalid.

Try calling it outside of its definition. (e.g. directly above list()).


#3

What do you mean ny "Try calling it outside of its definition"?


#4

Before you edited your code, you were calling add() inside of itself.

Kind of like this sort of thing:

var something = function() {
something();
};


#5

Thanks! I did a stupid mistake :grin: