Counting number of vowels in a string with JavaScript

前端 未结 18 1644
旧巷少年郎
旧巷少年郎 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:41

    One more method (using reduce):

       function getVowels(str) {
         return Array.from(str).reduce((count, letter) => count + 'aeiou'.includes(letter), 0);
       }
    

提交回复
热议问题