[removed] using typeof to check if string

前端 未结 6 2062
萌比男神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条回答
  •  萌比男神i
    2021-02-14 16:12

    You are checking keys of the object, not the value. It's usually a good practice to check against the constructor of an object to determine its type.

    Something like this:

    var languages = {
        english: "Hello!",
        french: "Bonjour!",
        notALanguage: 4,
        spanish: "Hola!"
    };
    
    for(i in languages) {
    
      if(languages[i].constructor === String) {
        console.log(languages[i])   
      };
    
    };
    

提交回复
热议问题