6. List Em All - really confusing


#1

Alright guys. Looks like this is a bit of a confusing one so thought i'd offer an explanation...

var friends = new Object (); <--new object created

friends.bill = { <---bill is created as an entry to friends-object, with details and an array-adress included.
'firstName': "Bill",
'lastName': "Gates",
'number': "202-555-5555",
'address': ["101 Hello Cresc", "Rodeo Drive", "WA", 97279],
};

friends.steve = { <--ditto for steve. added into the friends-object.
'firstName': "Steve",
'lastName': "Jobs",
'number': "309-555-5555",
'address': ["21 George Street", "Seattle Way", "WA", 87535]
};

var list = function (friends) { <---new function 'list' created, with a parameter of 'friends'
for (var friend in friends) { <---when list is called, we run the for-in loop-code here, which says a single var defined as
console.log(friend); <---'friend' that sits in friends - should be printed to the console.
};
};

loads of people getting confused by printing console.log then the name of the friend/s, plus the sudden addition of 'friend' singular (which you're effectively defining at that time.

hope that helps.


#2

I'm particularly confused by friend/friends, especially as it relates to the object (line 1) and the function (toward the end).

In the function, which of those can be substituted by terms like "pal", "pals", "buddy", or "buddies" for maximum clarity?

e.g.,

var list = function(friends) { //-- referring to the object?
    for(var pal in friends) { //-- new var in object?
        console.log(pal); //-- log new var?
    }

#3

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

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

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#4

+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


I dont know where is the problem
#5

+++ contact list explained
object description
search as function
for-in loop explained
http://www.codecademy.com/forum_questions/54a616f9937676fb3d0024c7