7. the code is correct but need explanation please


#1

var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "pots",
number: "123-123-123-123",
address: ['new york', 'bad city']
};
friends.steve = {
firstName: "Steve",
lastName: "hnun",
number: "321-321-312",
address: ['california', 'good city']
};
var list = function() {
for(var firstName in friends) {
console.log(firstName);
}
};
var search = function(name) {
for (var firstName in friends) {
if (friends[firstName].firstName === name) {
console.log(friends[firstName]);
return friends[firstName];
}
}
};
search('Steve');

please explain what this line doing, what is happening that translae it please-

if (friends[firstName].firstName === name)

my english is not very good so if its possilbe to explain like to a child.
the for (var firstName in friends) the above the code that i asked is finding the name 'Steve' that i called and then the if using it in the [] but i dont really get it what is happening there, please if somme1 understand what i mean, i just need to know what its doing exactly. why firstName in [] and not friends.firstName.firstName === name?
please hope some1 can explain me thanks alot!


#5

@psynnet,

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


#6

@psynnet,

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


#7

@psynnet,

+++ friends list and search and for-in explained.
Look here for an explanation
object
function
for-in-loop
list-function
search-function
http://www.codecademy.com/forum_questions/545275fd8c1ccc8e3e000a23#comment-5457b12c9c4e9d87cb00003f


#8

wow thanks very much


#9

Hi Leonhard - really appreciate that breakdown - are you able to clarify the following, as i thought i had this sussed but it's still very confusing for me...

var search = function(name) { <---we are defining a new function called search, with a parameter 'name'. Fine.
for (var firstName in friends) { <---we define a new variable called firstName in the friends object? or JS knows to
look for key firstName within friends within object? seems unlikely.
if (friends[firstName].firstName === name) { <---we're saying if friends' new firstName variable's firstName key is the
console.log(friends[firstName]); <--- same as 'name' parameter then etc. But how does JS know that this new var
**return friends[firstName];** <---we've just defined has any correlation to firstName within other keys within obj?

I guess what i really need to know is - are we defining a new variable called firstName in that 'for' loop?
Or does JS know to look for firstName 2 levels deep... where we're saying if(friends[firstName].firstname...

Because it feels like it should be written more like this, but it isn't:

var search = function (name) {
for (var friend in friends] {
if(friend[firstName] === name) {
console.log("friend")
return friend;
}
}
};


#10

@ade19801,

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