在 JavaScript 中返回字符串中的元音
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/2/22 8:44:17
我们需要编写一个 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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

