Why am I printing 4?


#1


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

Oops, try again. It looks like you printed 4 when you shouldn't have


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

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


#2

@krazykira,
Use

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

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

and you will see that your testing typeof on the property-key, which always is a "string"


#3

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