我们需要编写一个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