Programmers

[프로그래머스][JS] 모음 제거

winter17 2023. 4. 4. 22:54

https://school.programmers.co.kr/learn/courses/30/lessons/120849?language=javascript 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

풀이 과정

function solution(my_string) {
    let result = '';
    vowel = ['a', 'e', 'i', 'o', 'u']
    for (str of my_string){
        // str이 vowel 배열에 없다면
        if (!vowel.includes(str)){
            result += str
        }
    }
    return result;
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

 

Array.prototype.includes() - JavaScript | MDN

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

developer.mozilla.org


리뷰

includes 메서드를 사용하지 않았다면 for문을 두 번 돌려서 효율이 떨어질 뻔했다.

원래 문자열에서 모음을 삭제하면 공백이 생기는데 그걸 신경써줘야하는 줄 알았더니

result 빈 문자열 변수에 모음이 아닌 문자만 누적합시켜도 통과가 되었다