codewars 7kyu Vowel Count
这个题的意思是,给一个字符串,计算字符串中元音字母( aeiou )的个数,输入只包括小写字母和空格。
我写的:
function getCount(str) {
var vowelsCount = 0;
// enter your majic here
for (let i = 0; i < str.length; i++) {
if (
str[i] == "a" ||
str[i] == "e" ||
str[i] == "i" ||
str[i] == "o" ||
str[i] == "u"
) {
vowelsCount++;
}
}
return vowelsCount;
}
看看大佬们一行结束的代码:没错,大佬们一行就够了。
function getCount(str) {
return (str.match(/[aeiou]/gi) || []).length;
}
function getCount(str) {
return str.replace(/[^aeiou]/gi, "").length;
}
function getCount(str) {
return str.split("").filter((c) => "aeiouAEIOU".includes(c)).length;
}
const getCount = (str) => str.replace(/[^aeiou]/g, "").length;