Vowel Count
题目:
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.
翻译(来自有道翻译):
返回给定字符串中元音的数量(count)。
我们将把a、e、i、o和u作为这个形的元音。
输入字符串将只由小写字母和/或空格组成。
个人解题代码:
//思路:先创建一个用来计数的vowelsCount,for循环遍历str
//使用charAt()方法获取字符串指定位置的字母并与元音字母进行比较
//当有元音字母时计数变量自增最后输出自增变量vowelsCount
function getCount(str) {
var vowelsCount = 0;
for(var i = 0 ; i < str.length ; i++){
if(str.charAt(i)=="a" || str.charAt(i)=="e" || str.charAt(i)=="i" || str.charAt(i)=="o" ||str.charAt(i)=="u"){
vowelsCount++
}
}
return vowelsCount;
}
高亮答案:
function divisors(integer) {
var res = []
for (var i = 2; i <= Math.floor(integer / 2); ++i) if (integer % i == 0) res.push(i);
return res.length ? res : integer + ' is prime'
};