알고리즘 31 - Disemvowel Trolls

박진현·2021년 7월 17일
0

Q.

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!".

Note: for this kata y isn't considered a vowel.

A)

function disemvowel(str) {
  let result = '';
  for(i=0;i<str.length;i++) {
    if(str[i].toLowerCase() === 'a'||
       str[i].toLowerCase() === 'e'||
       str[i].toLowerCase() === 'i'||
       str[i].toLowerCase() === 'o'||
       str[i].toLowerCase() === 'u') {
      continue; 
    }
    else {
      result += str[i]
    }
  }

  return result;
}

regex 사용하면

function disemvowel(str) {
  return str.replace(/[aeiou]/gi,'')
}
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글