题目:
Return the number (count) of vowels in the given string.
We will consider a, e, i, o, and u as vowels for this Kata.
The input string will only consist of lower case letters and/or spaces.
个人感觉较好的解答:
function getCount(str) {
return (str.match(/[aeiou]/ig)||[]).length;
}
function getCount(str) {
return str.replace(/[^aeiou]/gi, '').length;
}
function getCount(str) {
return str.split('').filter(c => "aeiouAEIOU".includes(c)).length;
}
自己的解答:
function getCount(str) {
var vowelsCount = 0;
var vowels = 'aeiou';
for(var i = 0; i < str.length; i++){
if(vowels.includes(str[i])){
vowelsCount ++;
}
}
return vowelsCount;
}

本文介绍了一种计算给定字符串中元音字母数量的方法,提供了多种JavaScript函数实现方案,包括正则表达式匹配、字符串替换及数组过滤等技术。
523

被折叠的 条评论
为什么被折叠?



