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.