JavaScript 陣列中母音字母的數量


我們需要編寫一個 JavaScript 函式,該函式接受一個字串陣列(它們可以是單個字元或大於單個字元)。我們的函式應該只計算陣列中包含的所有母音字母。

示例

讓我們編寫程式碼:

const arr = ['Amy','Dolly','Jason','Madison','Patricia'];
const countVowels = (arr = []) => {
   const legend = 'aeiou';
   const isVowel = c => legend.includes(c);
   let count = 0;
   arr.forEach(el => {
      for(let i = 0; i < el.length; i++){
         if(isVowel(el[i])){
            count++;
         };
      };
   });
   return count;
};
console.log(countVowels(arr));

輸出

控制檯中的輸出將如下所示:

10

更新於:20-11-2020

650 次瀏覽

開啟你的 職業生涯

完成課程認證

立即開始
廣告
© . All rights reserved.