CodeWar(JavaScript)---Vowel Count

9 篇文章 0 订阅

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'
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值