List em all for/in


#1

var friends = {
bill: {
firstName: "Bill",
lastName: "Bob",
number: "888-888-88888",
address: ['111 first st']
},
steve: {
firstName: "Steve",
lastName: "Johnson",
number: "777-777-7777",
address: ['222 second st']}
}

var list = function (friends) {
for (var key in object) {
console.log(key);
};
}

I am getting "did you call a function called list?"?


#2

@webmaster34593,

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

#3

@webmaster34593,
+++++ 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] );


#4

@webmaster34593,

If you define your list =function=
like

var list = function (friends) {
         // Begin of FUNCTION-BODY
         //list-function has 1 PARAMETER =friends=
         // =friends= PARAMETER is used as a VARIABLE troughout FUNCTION-BODY
         // =friends= is a =local= VARIABLE to this function

};

If you want to call the list =function=
list();
and the list =function= was defined as having 1 PARAMETER =friends=
you will have to provide 1 ARGUMENT
in this case that would be the object Value friends
list(friends);


#5

what a great explanation - exactly what I needed. thank you!


#7

Thank you for going into such depth. It was a bit confusing at first, but I appreciate it.


#8

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "99918",
address: ['Microsoft', 'Redmond','WA', '98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "1234567",
address: ['123', 'Palo Alto', 'CA 94301']
}
};

var list=function(friends){

for( var i in friends ){
console.log(friends[i]);
}
}

list(bill);

the above code print out all the the object properties..of both bill and steve. Can u please help me out.


#9

make the console print (friends) instead of (friends[i]), and list(friends), since bill would come out as not being defined.