[removed] using typeof to check if string

前端 未结 6 2068
萌比男神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:30

    Here is the answer: (use typeof and then the object name followed by the var in your for statement and test whether it is equal to "string")

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

提交回复
热议问题