CountVowels [元音字母计数]
# 介绍
给定一串单词或短语,计算元音字母的数量。
# 实现
# JavaScript
/**
* @function countVowels
* @description Given a string of words or phrases, count the number of vowels(元音).
* @param {String} str - The input string
* @return {Number} - The number of vowels
* @example countVowels("ABCDE") => 2
* @example countVowels("Hello") => 2
*/
const countVowels = (str) => {
if (typeof str !== "string") {
throw new TypeError("Input should be a string");
}
const vowelRegex = /[aeiou]/gi;
const vowelsArray = str.match(vowelRegex) || [];
return vowelsArray.length;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
编辑 (opens new window)
上次更新: 2022/10/20, 20:03:22