[removed] using typeof to check if string

前端 未结 6 1913
萌比男神i
萌比男神i 2021-02-14 15:32

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

6条回答
  •  死守一世寂寞
    2021-02-14 16:21

    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); 
        }
    }
    

提交回复
热议问题