We made a friend


#1


https://www.codecademy.com/courses/building-an-address-book/0/6?curriculum_id=506324b3a7dffd00020bf661#


Oops, try again. It looks like there are still only two items in the contacts array.


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(contacts[i].lastName===lastName)
        {
            printPerson(contacts[i]);
        }
    }
};
search("Jones");
var add = function(firstName,lastName,email,phoneNumber){

        this.firstName=firstname;
        this.lastName=lastName;
        this.email=email;
        this.phoneNumber=phoneNumber;
    contacts[contacts.length]= new add();
};
add("hello","bye","codecacademy@gmail.com","055-58586969");


#2

here:

contacts[contacts.length]= new add();

new add(); wont work, this contains the current contact you are trying to add, so try adding this to contacts array


#3

sorry i dint get you


#4

well, the new keyword is for creating a new array or object:

new Array();
new Object();

not for calling a function. So to insert the contact into contacts array, you will need to find a different way. Think about what the this keyword does


#5

Oops, try again. It looks like you didn't add an person object to the end of your contacts array

This is the error I kept getting, result displays well apart from an undefined firstname and lastName that shows before the newlt added name. what do i do abou that pls


#6

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

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 < contactsLength; i++){
    if (lastName === contacts[i].lastName){
     printPerson(contacts[i]);   
    }
}
}*/


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

add("kelechi", "Ugwu", "23480032465303", "kcblaq@kcblaq.com");


list();

#7

here:

var contacts = [bob, mary, add];

add is undefined. inside the add function you add the contact to your contacts array. Remove add from your array


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.