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


"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?



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;

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


for(var x in obj) {

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") {

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


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