Counting number of vowels in a string with JavaScript

前端 未结 18 1634
旧巷少年郎
旧巷少年郎 2020-12-08 23:53

I\'m using basic JavaScript to count the number of vowels in a string. The below code works but I would like to have it cleaned up a bit. Would using .includes()

18条回答
  •  悲&欢浪女
    2020-12-09 00:23

    Function vowels(str){
       let count=0;
       const checker=['a','e','i','o','u'];
       for (let char of str.toLowerCase){
          if (checker.includes(char)){
            count++;
          }
       return count;
    }
    
    
    Function vowels(str){
       const match = str.match(/[aeiou]/gi);
       return match ? match.length : 0 ;
    }
    

提交回复
热议问题