7kyu
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.
핵심 요구 사항 : 모음을 제거한 문장을 return하기
찾을(기준)배열.includes(비교배열);I'll explain the concepts behind the Codewars problems
and how to approach them effectively
배열명[인덱스위치] = 지정값;으로 function disemvowel(str) {
let resultArr = str.split("");
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
for(let i=0; i<resultArr.length; i++){
if(vowels.includes(resultArr[i]) === true){
resultArr[i] = "";
}
}
return resultArr.join("");
}
function disemvowel(str) {
return str.replace(/[aeiou]/gi, '');
}
1) replace 함수
문자열.replace([정규식],[대체값]);
2) 정규식 개념
[aeiou] : 대활호 안의 문자들은 '문자 클래스'로 소문자 a,e,i,o,u 중 하나와 일치하는 것을 의미
g 플래그 : 글로벌 플래그로, 문자열 전체에서 해당하는 패턴과 일치하는 모든 부분을 찾습니다. g가 없으면 첫번째모음만 찾고 치환합니다. (global의 약어)
i 플래그 : 대소문자 구분 없이 찾는 것. 즉 대문자도 포함된다. (ignore case)
슬래시(/) : /는 정규 표현식의 시작과 끝을 나타내는 구문입니다.
정규식을 정의할 때, 슬래시로 패턴을 감싸는 것이 JS에서 일반적인 문법
즉, /(정규표현식의 시작) aeiou /(정규표현식 끝) g(글로벌 플래그) i(대소구분없는 ignore 플래그) 로 => replace에서 찾을 문자열에 대해 간단하게 찾을 수있다.
정규표현식 이해하기 : 문제를 풀면서 익히니까 기억에 잘 남는다.
배열 메소드 : inclues가 있다는걸 알더라도 실제로 적용해보지 않으면 제대로 사용할 수 없는 것 같다.
처음엔 반복문으로 치환하는 방법만 생각했지만, 정규표현식을 사용하면 더 짧고 간결하게 문제를 풀 수 있어 시간을 매우 단축시킬 수 있다. 왜 사용하는지 잘 몰랐는데 다른 사람들의 풀이를 보고 다양한 풀이방법을 배워야겠다..