6. We Made a Friend!



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++) {

/*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) {
        else {
            console.log("No contact found");



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



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++)

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


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
but as you have defined the add() function as having 4 parameter's
you will have to provide 4 arguments

You can now check your work
with a

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



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:


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.


function add(firstName, lastName, email, phoneNumber)
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email


This was an incredibly poorly written last lesson.