We made a friend! my code doesn't work


#1

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

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(firstName){

for (var i = 0; i < contacts.length; i++) {
    if(contacts[i].firstName===firstName){
        printPerson(contacts[i]);
        
        }
    		   
};

};

var add = function(firstName,lastName,email,phoneNumber){
var borja = {
firstName: "Borja",
lastName: "Johnson",
phoneNumber: "(650) 788-8888",
email: "borja.johnson@example.com"

    };

};
add(firstName);

HOW HAS TO BE MY CODE FOR WORKING RIGHT?


#2

What is the error you are getting? Also, please paste a link to the actual exercise.


#3

@boardblaster58454

Remove borja in your array. Borja doesn't exist yet. You must add her with the add function.

You're not suppose to add Borja in the add function. Remember, this function must help us to add different friends to our address book every time we call the add function. Here, you are just adding the Borja friend. So remove all these line:

Instead, put this

contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
}

Which will add your friend to contacts by passing the value of parameter inside contacts.

Do not add firstName otherwise it will be undefined because firstName is not a variable you created. Instead, to add Borja, fill in the parameter. Example:

add("firstName", "lastName", "example@mydomain.com", "123456");

Which will add firstName lastName (the full name of the person) with email example@mydomain.com and phone number 123456.


#4

thanks!!!! I understood that at the end :slightly_smiling:

I removed , Borja from the array, and I put my code like this:

var add = function(firstName, lastName, email, phoneNumber) {

var contact = {

firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
contacts[contacts.length] = contact;
};

add("Dude", "Dudeson", "email.com", "0000000");
add("Borja", "Gonzalez", "email.com", "123456");
list();

I UNDERSTOOD THIS: I was CREATING A FUNCTION for adding A NEW FRIEND.
So, at first a create a object witch the same properties that i put like arguments in the add function.
Secondly, I put the new object at the end of the array with mary and bob. with: contacts[contacts.length] = contact;
Finally, with add function I give a new contact to the book with the value of properties that I wanted.