27. Looks For-In To Me


#1



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


"Examine the languages object. Three properties are strings, whereas one is a number." - from the exercise.

I am completely stumped by this one. I don't understand how there are supposedly three of them but when I do the 'typeof" on 'x' then it tells me they're all strings. I don't quite understand how to only print the greetings of "Hello" in the three languages. I was thinking isNaN?


//ORIGINAL TRY THAT DIDN'T
//WORK. IT PRINTED ALL THE
//VALUES OF LANGUAGES
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

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




//SECOND TRY THAT
//TELLS ME THEY'RE
//ALL STRINGS
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var x in languages) {
    console.log(typeof x);
}


#2

Naaa....You need to set a condition to know what typeof variable is x in languages.

for (var x in languages) {
    if (typeof languages[x]==="string"){
        console.log(languages[x]);
        }
}

#3

Ohhh okay. I would have never thought of using an if statement. Now that you said it it seems obvious. Thank you!


#4

You're welcome.

Have fun coding! :slight_smile:


#5

@gulnaw

In your Original post
with x you where testing the property-key's and NOT the associated Value's

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


#6

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