7/8 why is a for loop necessary?


#1
var friends = {};
friends.bill = {};
friends.steve = {};
friends["bill"]["firstName"] = "Bill";
friends["bill"]["lastName"] = "Gates";
friends["bill"]["number"] = "(206) 555-5555";
friends["bill"]["address"] = ["One Microsoft Way", "Redmond", "WA", "98052"];
friends["steve"]["firstName"] = "Steve";
friends["steve"]["lastName"] = "Jobs";
friends["steve"]["number"] = "(408) 555-5555";
friends["steve"]["address"] = ["1 Infinite Loop", "Cupertino", "CA", "95014"];

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

function search(name) {
    if (friends[name]) {
        console.log(friends[name]);
        return friends[name];
    }
}

search("steve");

with this code, i got the correct output, but the system wouldnt let me proceed until i added a for loop.

so i put in a for loop just to make codecademy happy, but im not sure why it was really necessary?


#2

@blackbuddha88,

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

@blackbuddha88,

What they are trying to achive in the search function....
that you are going through the friends object
on the lookout for a =name= given as an argument
and _compare the given argument
to the =firstName=-property of every =friend= in the friends object......

So your function should either be called
like

 search("Steve");

or

 search("Bill");

#4

@blackbuddha88,

var friends= {
              bill: { firstName: 'Bill',
                      lastName: 'Gates',
                      number: '444-444-4444',
                      address: [ 'One Microsoft way', 'Redwood', 'WA' ]
              },
             steve: {
                      firstName: 'Steve',
                      lastName: 'Jobs',
                      number: '333-333-3333',
                      address: [ 'Two infinite loop', 'Bentown', 'DV' ] 
              }
   };

keep repeating these two lines
An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated value

Description of the friends object.
The friends object has 2 properties and are seperated by a comma-,

there is a bill property with property-key bill and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)
there is a steve property with property-key steve and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)

access via dot-notation == only literal property-key allowed

friends.bill ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends.bill.lastName ==> you will get the associated value of the lastName property-key

access via the square-bracket-notation == literal & VARIABLE

1 using the literal property-key

friends["bill"]
friends["bill"]["lastName"]

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "bill";
var propertyKey2 ="lastName";
friends[propertyKey1] ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends[propertyKey1][propertyKey2]
in this case you will get the string VALUE of the lastName property
of the associated object VALUE of bill