6. We Made a Friend!


#1



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


How can I complete 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() {
    for (var i = 0; i < contactsLength.length; i++) {
        printPerson(contacts[i]);
    }
}

/*Create a search function
then call it passing "Jones"*/
function search(lastName)
{
    for (var i = 0; i < contacts.length; i++) {
        if (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
    }
        else {
            console.log("No contact found");
    }

}

}
search("Jones");

function add(firstName, lastName, email, phoneNumber) {
    var james = {
        firstName: "james",
        lastName: "Jones",
        phoneNumber: "(651) 777-7777",
        email: "james.jones@example.com"    
    };
    
    contacts.james();
};

add(james.firstName);


#2

@nsanjay,
In your list() function
you should change your FOR-loop setup
from your

(var i = 0; i < contactsLength.length; i++)

either into

(var i = 0, contactsLength = contacts.length ; i < contactsLength; i++)

OR
`
(var i = 0; i < contacts.length; i++)


#3

@nsanjay,
Concerning your add() function
you should know that if the function-definition has parameters within its parameter-holder's-( )
the mentioned parameters are used as =local= variables within the function-BODY.

Thus defining the add() function like

function add(firstName, lastName, email, phoneNumber) {
       //Begin of add() FUNCTION-BODY
       // we can use 4 =local= VARIABLE's
       // firstName , lastName , email  , phoneNumber

       // we want to create a =new= contact-object
       var newContact = {
                   firstName: firstName,
                   lastName: lastName
                   };
       //we want to =append= this =new= contact-object
       // to the allready existing =contacts= Array
       contacts[contacts.length] = newContact;

       //End of FUNCTION-BODY
      }

Now if you want to add a =new= contact
you can call the add() function
like
add()
but as you have defined the add() function as having 4 parameter's
you will have to provide 4 arguments
like
add("JJ","Cale","JJ.cale@heaven.org","911");

You can now check your work
with a

list();
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

:eyeglasses:


#4

Don't hardcode anything inside your add function. As step 2 says, " Instead of having this object's property values be filled with strings though, set them to the appropriate function parameters passed in." Create a generic variable newContact instead of james. After you created that variable, I followed it with the push function in JS. You get the contacts array and 'push' or insert the newContact inside the array:

contacts.push(newContact);

Then when you call the add function, provided you give it string arguments, it creates a newContact object and pushes that object inside the contacts array. Follow it up with the list() function to see your results.


#5

function add(firstName, lastName, email, phoneNumber)
{
contacts.push(
{
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
})
};


#6

This was an incredibly poorly written last lesson.