"Looks For-In To Me" - number distinction in a For-In loop


#1

"Use a for-in loop to print out the three ways to say hello. In the loop, you should check to see if the property value is a string so you don't accidentally print a number."

Well, on the one hand, all properties inside the For-In loops are considered string-typed, on the other hand, numbers should not be printed. how can I distinct numbers while every property in a For-In loop becomes a string?

Itay.


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


Error I get:
Oops, try again. It looks like you didn't print Hello!


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

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

EDIT Sorry!! I figured it out! seems like I should've used the associative value and not the property one!


#2

for(var x in obj) {
executeSomething();
}

This will go through all the properties of obj one by one and assign the property name to x on each run of the loop.

Now the loop

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

You don't need to declare a separate variable to act as a placeholder
The variable in that loop could very well be 'l' or 'z'

What's important is that you reference the language in languages inside the loop, not languages itself
Thats where languages[x] comes into play


#3

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