My code works and I dont understand why


#1

I get confused after the if statement. I changed my code 5 times before I got it to work.

Why does it have to be printPerson(bob)

why can't it be printPerson(lastName) ?

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(bob);
        }
    }
}
search("Jones");

#2

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#3

A
console.log( contacts );
would have revealed that you have created an Array with 2 elements
and each of those elements is an object

[ { firstName: 'Bob',
    lastName: 'Jones',
    phoneNumber: '(650) 777-7777',
    email: 'bob.jones@example.com' },
  { firstName: 'Mary',
    lastName: 'Johnson',
    phoneNumber: '(650) 888-8888',
    email: 'mary.johnson@example.com' } ]

console.log( contacts[0].firstName ); ==>output>> Bob
console.log( contacts[1].email ); ==>output>> 'mary.johnson@example.com'
console.log( contacts[1] ); ==>>output>> the Mary-Object

As your printPerson function takes 1 parameter person
the printPerson function will expect 1 argument being the contacts[i] object
which you have just found by comparing contacts[i].lastName with the search argument 'Jones'.

PS.
It might be that you get a double-output,
as the course-checker also has it's output in your Display.
Add following line to the end-of-code.....
console.log( "This the END-of-my-CODE" );
and you will be able to see what is yours and what is the =code-checker=.