Getting IN-timate


#1

So when I wrote the code below and run it, it allowed me to pass, have a look :

not soo sure that is correct because is not printing the values of fullName etc.
or I just don't understand what should I do in this excersice :confused:


#2

@ronin85,

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


#3

I was wondering the same thing, but it looks like they did ask us to print out the PROPERTIES and not their values, so I think we're golden. :wink:


#4

@kat.timm,
The property-key
and not its associated Value


#5

Yes, I did figure that out. Thanks!


#6

Your like the Gandalf of code academy :wink:


#7

@leonhard.wettengmx.n So then conceptually, would it be correct to think of the method's assigning the key-string to the variable as the default scenario and think of the method's assigning the value-string as an else if case (user specifies object[variable])?


#8

@msfrisby,

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 at every iteration over the properties ( inclusive those found in its prototype-chain )
you have a variable-name which has a property-key as a String assigned to it.

Now as you are on the HUNT for the associated Value of the mentioned property-key
you can only use the so-called bracket-notation,
like
nyc[x]

==getting == at == the == associated Value =========================

var james = {
    job: "programmer",
   married: false,
   sayJob: function() {
          // complete this method
          console.log("Hi, I work as a" + this.job);
    }
 };

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

there is a job property with property-key job and it's associated string value 'programmer'
there is a married property with property-key married and it's associated boolean value false
there is a sayJob property with property-key sayJob and it's associated anonymous function VALUE
( they also would 'say', the james-object has the sayJob()-Method )

access via dot-notation

james.job ==> you will get the associated string VALUE of the job property-key, thus
you get the string VALUE 'programmer'

james.married ==> you will get the associated boolean VALUE false

james.sayJob ==> you will get the associated anonymous function VALUE
to call/execute this method you add a pair of parenthesis-( )
james.sayJob();

access via the square-bracket-notation

1 using the literal property-key

james["job"]
james["married"]
james["sayJob"]()

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

var propertyKey1 = "job";
var propertyKey2 = "married";
var propertyKey3 = "sayJob";
james[propertyKey1] ==> you will get the associated string VALUE of the job property-key, thus
you get the string "programmer"

james[propertyKey2]
in this case you will get the boolean VALUE of the married property
being false

james[propertyKey3]();
would =display= "Hi, I work as a programmer"


Why does bracket notation for accessing object properties work where dot notation doesn't?
Bleep Bleep: Variable is to Global Variable as This.Key is to...?
#9

@leonhard.wettengmx.n

Thank you, I think I understand better now.

So for-in is a built-in Method that is programmed to assign the key as a string value to the variable.

for (var variable in obj) { obj } // "key"

Unless I put the same thing but add the dynamic value parameter(?) in bracket notation:

for (var variable in obj) { obj[variable] } // "value"

in which case it will assign the value as a string-value to the variable.

Is that right?


#10

@msfrisby
Having an object

var obj = {
    property-key: "associated-value"
   };

With a FOR-IN loop

for ( var theKey in obj ) {
     //you have the objects =property-key= in the VARIABLE =theKey=
     //if you are only interested in the =property-key=
     //you would use =theKey=
     console.log( obj.hasOwnProperty( theKey ) );
     //however if you are interested in 
      //the =associated-value= of the =property-key=
     //you will have to use the =bracket-notation=
     //like
     console.log( obj[ theKey ] );
}

#11

I see! Thank you so much for explaining it in several different ways @leonhard.wettengmx.n! I've started to get the hang of it :nerd: