Loops


#1

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

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

//Why it's work/

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

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

//And this isn't.


#2

Hi,
In you second code, you used languages.part
So the computer do what you asked : search the type of language.part. Literally
The issue is language.part does not exist. There's no property called "part"

What you want is to use language.[the name of a property], right ?
But how to do it without typing each property ?

Well, in your first code, you wrote language[part] .
This is very different. With this notation, the computer does not search a property called part,
Instead, it search for language.[the value of "part"].
And the value of part is the string associated with each of your property.
So, language[part] = language.english ; language.french ; language.spanish