LoginSignup
1
0

More than 5 years have passed since last update.

js 与えられた文字の母音の数を返す

Last updated at Posted at 2018-05-11

与えられた文字の母音(a i u e o)の数をかえしてください。
*ここで与えられる文字はスペースを含むアルファベットを指します。
*正規表現、使用不可(簡単すぎるので)


getCount = (str)=> {
  //write your code

}
getCount('abcidsge');//3

使ったもの

forEach()
if
spread operator

考え方

spread operatorで引数を一文字にして配列にいれる。
配列の文字をforEachで個別にifで母音か判定
当てはまれば返す数字を足していく

コード

getCount = (str)=> {
  let vowelsCount = 0;
  str.toLowerCase().split('').forEach((e,i)=>{
    if(e == "a"||e == "e"||e == "i"||e == "o"||e == "u")vowelsCount++
  });
  return vowelsCount;
}
getCount('abcidsgE');

その他コード

function getCount(str) {
 return str.split('').filter(c => "aeiouAEIOU".includes(c)).length;
}

他にもコードが浮かんだ方、
コードゴルフが浮かんだ方、コメントにお願いします。

おまけ 正規表現

function getCount(str) {
  return (str.match(/[aeiou]/ig)||[]).length;
}

1
0
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0