Get stucked


#1


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


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

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


#2

x is the key:

for(var x in languages) {
   console.log(x);
}

while you need to compare the value of your dictionary. You can use keys to access values in a dictionary:

languages[x]

and of typeof is a string, it gives "string", so you should compare with "string"


#3

i just tried something like this:

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

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

#4

this is a big improvement, but on this line:

if(typeof(x) == "string")

x is still the key, you need the dictionary value


#5

Thx I just figured out what's wrong.


#6

But I can not realize how these types of stuff work:

if(typeof(languages[x]) !== "number")

or

if(typeof(languages[x]) !== "string")

?
I mean what's between quotes must be a string..
But JS recognise it as a type of data..
It's pretty strange.


#7

typeof is a function which returns the data type as string:

console.log(typeof(3)) # "number"
console.log(typeof("test")) # "string"
console.log(typeof(true)) # "boolean"

then you can use == or != to compare the data type as string returned by typeof with a data type of your choice


#8

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