19

I'm working on a codecademy.com exercise where we use for-in statements to loop through an object and print hello in different languages by checking to see if the values of the properties in the languages object are strings using typeof

my check to see if the value is a string is not working. my loops giving me this result

english
french
notALanguage
spanish

The code

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

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

These are the instructions for the exercise

Objects aren't so foreign if you really think about it!

Remember you can figure out the type of a variable by using typeof myVariable. Types we are concerned with for now are "object", "string", and "number".

Recall the for-in loop:

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.

Let's combine our knowledge of these two concepts.

Examine the languages object. Three properties are strings, whereas one is a number.

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.

2
  • 2
    it's possible to pose a question without using a question mark (?) and without using the typical syntax of a question. The OP showed what my problem was, and two people who answered (and all the people who upvoted) understood it clearly. Commented Aug 28, 2012 at 6:57
  • 1
    yes, you've given the code and the corresponding obvious output. It's a good idea to tell what your expectation was, since the code works perfectly valid. Commented Aug 28, 2012 at 7:11

6 Answers 6

39

That's because you're checking the key of the object. To check the actual value, you should be doing something like object[key]. Try this:

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

// print hello in the 3 different languages
for(var hello in languages){
    var value = languages[hello];
    if (typeof value === "string"){
    console.log(value); 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

Here is the answer: (use typeof and then the object name followed by the var in your for statement and test whether it is equal to "string")

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

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

Comments

2

You are checking keys of the object, not the value. It's usually a good practice to check against the constructor of an object to determine its type.

Something like this:

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

for(i in languages) {

  if(languages[i].constructor === String) {
    console.log(languages[i])   
  };

};

1 Comment

Excellent. One advantage of comparing against an identifier instead of a string literal is that the latter won't throw an exception if there's a typo, whereas the former will. Still looks a bit clunky but the best option I found so far. Upvoted.
2

"string" is not the same as String. I've found one way to test typeof for string.

if (typeof something === typeof "test") ...

You can of course use "string" to compare to, but any actual String will do.

1 Comment

At first I didnt understand what you were saying, but now I see what you are doing. Great thinking. This way you can really be sure its a string value your finding. For clarity you could improve it slightly and say: if (typeof something === typeof "string") ..
1

this is the for in value to work for me

for(var x in languages){

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

Comments

0

The below coding is also useful to perform only string value.By using variable to access the property list of abject after that by using it check the value is a NotANumber by using isNaN.The code given below is useful to you

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

// print hello in the 3 different languages
for(a in languages)
{
if(isNaN(languages[a]))
console.log(languages[a]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.