9/30 Introduction to Objects II


#1

Need help....please write the code


#2
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

for (var ite in nyc){
console.log(ite)
}


#4

Can you help me understand....here is my code
var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};
for (var x in nyc) {
console.log(nyc[x]);
}

Codecademy gives me an error, saying it didn't print NYC's full name


#5

@bytewhiz45073,
= http://discuss.codecademy.com/t/9-30-introduction-to-objects-ii/1184/2?u=leonhard_wettengmx_n

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


#6

I was pretty sure they want me to print all property(with values), so I read again and now I see difference.
10 of 30 they want us to print the value of each property

I really appreciate your help, thank you again!


#7

@bytewhiz45073,
Most of your programming-life,
will be the hunt on Value's ( your Data )
The associated VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         â—¦Array
         â—¦Date
         â—¦RegExp
         â—¦Math
•Null
•Undefined

You will create Information
by manipulating and evalutating the collected Data into a particular FORMAT.


#8

My code is

var nyc = {
        fullName: "New York City",
        mayor: "Bill de Blasio",
        population: 8000000,
        boroughs: 5
    };
    
    for(var x in nyc){
        console.log(x);
    }

I am still getting error saying "It looks like you didn't print nyc's fullName" in popup and the output area says "TypeError: console.log is not a function". I am really confused.


#9

@iamripu,
In such a case you are talking-about a Browser hick-up

++++ reset of session *****************
As you are manipulating with objects
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).
CTRL 0 to reset browser zoom


#11

Question: How come any word used in a for-in loop works to get properties?


#12

@samuelhauptmannvanda,
did you read http://discuss.codecademy.com/t/9-30-introduction-to-objects-ii/1184/2?u=leonhard_wettengmx_n


#13

Jup. :smile:

Can you explain it to me so I can be sure what is going on? :smile: