Looks For-In To Me


#1


https://www.codecademy.com/courses/objects-ii/5/1?curriculum_id=506324b3a7dffd00020bf661#


My code is still printing out 4. I printed out the types and it is saying it is a string??


var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var property in languages){
    if(typeof property === 'string'){
        var x = property;
        console.log(languages[x]);
    };
};


#2

@digitalrunner03099,

- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it's associated value.

 var object1 = {
              name: "First"
              }

So object1 has 1 property
a name property with property-key name and it's associated string VALUE "FIRST"

OR

var myObj = {
         type: 'fancy',
         disposition: 'sunny'
        }

myObj has 2 properties separated by a comma-,,
a type property with property-key type and an associated string VALUE 'fancy'
a disposition-property with property-key disposition and
..an associated string VALUE 'sunny'.

===============================================================

Thus with your
for(var property in languages){
you will have the property-key in the variable property !!!!!
and a property-key is Always a string !!!!

thus as you will have to check the type of the associated Value
you will have to use
typeof languages[property] === 'string'


#3

Hey, I got a question for you, code-boss.

In this example:
...
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};

// print hello in the 3 different languages
for (var x in languages) {
if (typeof languages[x] === "string") {
console.log(languages[x]);
}
}
...

Why does the above work while

...

console.log(this.x);
...

or
...

console.log(this[x]);

does not?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.