在 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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP