27/30 It works but there must be a better way


#1

Ok my code works and and I made it to the next part. However there must be a better solution. I tried a bunch of much more clever solutions that didnt pan out. So I am wondering what you guys did to solve this problem.

Here is my code

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

// print hello in the 3 different languages
for (var x in languages){
    
    var variable = "english";
    
   if (languages[x] == "Hello!" || languages[x] == "Bonjour!"|| 
         languages[x] == "Hola!"){
       
       console.log(languages[x]);
   }
   // console.log(languages[x]);
    
}

#2

Yes, there is...

if (typeof languages[x] === 'string'){
   
   console.log(languages[x]);

}


#3

Slightly different question.
How come (6/20)
var anObj = { job: "I'm an object!" };
var aNumber = 42;
var aString = "I'm a string!";

console.log(typeof anObj ); // should print "object"
console.log(typeof aNumber ); // should print "number"
console.log(typeof aString ); // should print "string"

prints out the type, but if i do something like

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

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

This doesn't work? The only difference I can see is that in the first example we're printing out the type of a variable, but in the second example we are looking at properties of an object....

Can someone shed some light?


#4

The typeof operator can suss out most data types. The data type is not of the variable, per se, but of the value or object to which the variable refers.

var a = Math.PI;
console.log(typeof a); // 'number'

In the above, a is not a 'number'; Math.PI is. If we examine a in the context of the window object, we will find that it is in fact a string.

for (var key in window){
    if (key === 'a') {
        console.log(typeof key);
    }
}

// 'string'

In other words, quoted or unquoted, all property names are strings, and by inference, all variable names (which are also property names) are strings.

The values associated with the property names may be any recognized type. If we examine the code that does not work for you, we can see why it does not work as expected:

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

In the above, propValue is the key, which will always be a string type. We should be writing,

    if (typeof languages[propValue] === 'string') { ... }

then it will work as expected, and not print the 4.


#5

Thanks @mtf . I read through your answer a few times and the key sentence that hit home for me was "The data type is not of the variable, per se, but of the value or object to which the variable refers."

I had the solution working before sending the question because I couldn't get my head around why I needed to refer to the value of the property - since in 7/20 I accessing the property and printing that out to the console, so I thought my initial piece of code should be enough.

I think I understand what you mean though....its the difference between accessing the property or the value of the property of the the object.

Thanks for the quick reply.


#7

A post was split to a new topic: 27/30 Question about another solution