27. Looks For-In To Me


#1

Here's my code:
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
//console.log(typeof languages.notALanguage);
// print hello in the 3 different languages
for(var x in languages) {
console.log(typeof x);
if (typeof x === "string") {
console.log(languages[x]);
}
}

The output is;
string
Hello!
string
Bonjour!
string
4
string
Hola!

So, my IF condition will never work, because all of them are string types. Clearyly, TYPEOF will not work.

Any suggestions?


#2

You made mistake in your if statement the x is a key and then there is a value inside the key x. inorder to compair the value you have to mention the variable name and the key like this
if(typeof languages[x] === "string")


#3

What am I doing wrong? It always prints 4

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

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


#4

I figured it out. the ; after the for loop should have been removed


#5

console.log in your for in loop should have no parameters,tried that,worked like a charm,here is my code:
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};

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

console.log(languages[x]);
}

};