6. We made a new friend


#1

So my code works and everything looks fine, but when I try to log the new added contact, it tells me that it's undefined, and it wont log its properties. However, it does show that the new contact has been added to the contacts array, when list function is called. The code passes but how do I know if the properties are added or how do I access them? So it would log the properties of my new contact like it does for mary or bob!

Here is my code:

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

add("ash", "miri", "asdasd@ssdas.com", "129 32939");
list();
console.log(mary);
console.log(ash);

and this is the result:

Bob Jones
Bob Jones
Mary Johnson
ash miri
{ firstName: 'Mary',
lastName: 'Johnson',
phoneNumber: '(650) 888-8888',
email: 'mary.johnson@example.com' }
ReferenceError: ash is not defined

Could someone help please!


#2

Can you show us your whole code so i can help you pls??


#3

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"*/

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

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

add("ash", "miri", "asdasd@ssdas.com", "129 32939");
list();
console.log(mary);

thank you,