본문 바로가기
알고리즘/CodeWars

[CodeWars] javascript - 7kyu - Disemvowel Trolls 문제풀이

by TLOWAC 2020. 4. 27.

Title

Disemvowel Trolls

Description

Trolls are attacking your comment section!

A common way to deal with this situation is to remove all of the vowels from the trolls' comments, neutralizing the threat.

Your task is to write a function that takes a string and return a new string with all vowels removed.

For example, the string "This website is for losers LOL!" would become "Ths wbst s fr lsrs LL!".


How Can I Solved

문제 요구사항 정의

인자로 입력되는 문자열 중에서 자음만을 반환하라.

문제 접근

문제의 요구사항에 따라서 자음을 구해도 되고, 모음을 없애도 된다.

나는 모음을 없애는 방법을 사용할려고 한다.
모음의 갯수가 자음의 갯수보다 현저히 적기 때문이다.
( 모음 : a, e, o, u, i)

정규표현식

모음은 총 5개로 a,e,o,u,i 이다.
[] 대괄호 안에 모음을 담아서 처리 한다.

정규 표현식에서 [a,e,o,u,i]는 a 또는 e 또는 o 또는 u 또는 i 가 있는 경우 선택하는 것 이다.

str.replace(/[a,e,o,u,i]/gi,'')

replace

str 문자열의 특정 부분을 ''로 바꾼다. 즉, 없앤다.

str.replace(/[a,e,o,u,i]/gi,'')

Solution

function disemvowel(str) {
  return str.replace(/[aeiou]/gi, '');
}

댓글