2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

js 与えられた文字から母音を切り取ってかえす

Posted at

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


disemvowel = (str) => {
  //write your code.
}
disemvowel('Your task is to write a function that takes a string and return a new string with all vowels removed.');
//"Yr tsk s t wrt  fnctn tht tks  strng nd rtrn  nw strng wth ll vwls rmvd."

使ったもの

spread operator
filter();
join();

考え方

spread operatorで分割した文字を配列にいれる。
filterで文字が母音であるか判定して、偽であれば返すようにする。
join()して返しておわり

コード

disemvowel = (str) => {
  let trimmedStr = [...str].filter(e=>{ return 'aeiouAEIOU'.includes(e)?'':e});
  return trimmedStr.join('');
}
disemvowel('Your task is to write a function that takes a string and return a new string with all vowels removed.');//

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

おまけ 正規表現

disemvowel = str => str.replace(/[aeiou]/gi,'');
2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?