(27) Doesn't work with dot notation


#1

I already solved it with the bracket way, then tried using dot notation. Here is the code:

 var languages = new Object();
    
    languages.english = 'Hello';
    languages.french = 'Bonjour';
    languages.spanish = 'Hola';
    languages.number = 1;
    
    for (var one_lang in languages) {

// So, lets say we are in the for loop, and for testing purposes 'one_lang" is 'english'.
When I try to print out 'languages.one_lang' I got the message that is 'undefined', which is strange.
Its same as saying 'languages.english'. Can someone explain why it doesn't work?

    console.log(one_lang); //print 'english'

    **console.log(languages.one_lang);**

//This part doesn't work anyway

if(typeof languages.one_lang === 'string') {
    console.log(languages.one_lang);
    }
else {
    continue;
    }
}

#2

you can leave out the else block, i dont think continue works like that, as this isn't going through a loop


#3

When using the dot method, "one_lang" does not get resolved.
So it tries to literally get languages.one_lang, which does not exist in your object.
This would be the same as asking for languages["one_lang"], which will also not work.


#4

boss your code won't work because of the dot notation you are not supposed to use dot notation for iteration. remember your did not creat a one_language variable as an instance of any class.
i did something like below to yur code and it worked
for (var i in languages) {

console.log(i); //print 'english'


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

}

}