在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
热门推荐
10 给亲戚祝福语大全简短
11 给未来的祝福语简短
12 教师送给家长祝福语简短
13 儿子病好祝福语大全简短
14 订婚祝福语简短100字
15 小乖生日祝福语 简短
16 大专招生老师祝福语简短
17 英语笔译考试祝福语简短
18 年终大会祝福语简短霸气