We made a friend


#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];

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]);
};
}
var add = function(firstName, lastName, email, phoneNumber) {
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
};
add("Cem", "Ucar", "example@hotmail.com", "89531436");
list();

What's wrong ?


#2

@methodpro99496

Pass in parameter in this function contacts so that it saves your contact information inside of the array.


#3

Make sure that the search() function still exists! Your code threw the following error when checking if you called search(): This is my problem. I dont understand what i'll do :frowning:


#4

@methodpro99496
Can you print out the code again inside of the discussion so I can check what is wrong?


#5

same problem. I checked other solutions ,but i took same problem :frowning:


#6

It looks as though the exercise from this is missing. Add this inbetween the list() function and the add function

function search(lastName){
var contactsLength = contacts.length;

for(i = 0; i < contactsLength; i++){
    if(lastName === contacts[i].lastName){
        printPerson(contacts[i]);
    }
}

} ;

search("Jones")


#7

Actually you just need this line:

var contactsLength = contacts.length;

You have to reset the contactsLength to get the list function looping till the 3rd person.

I just had this problem.
Thank you for the insight.


#8

//O meu funcionou assim//

var add = function (firstName, lastName, phoneNumber, email) {
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
};

add ("Loki","De Ploc","(55) 9999-9999","lokideploc@example.com");

list();