在 JavaScript 中返回字符串中的元音


我们要求编写一个 JavaScript 函数,该函数采用可能包含某些字母的字符串。该函数应计算并返回字符串中存在的元音数。

实例

以下是代码 −

const str = 'this is a string';
const countVowels = (str = '') => {
   str = str.toLowerCase();
   const legend = 'aeiou';
   let count = 0;
   for(let i = 0; i < str.length; i++){
      const el = str[i];
      if(!legend.includes(el)){
         continue;
      };
      count++;
   };
   return count;
};
console.log(countVowels(str));

输出

以下是控制台上的输出 −

4

更新时间: 11-Dec-2020

639 浏览量

开启您的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.