TIL_프로그래머스 - Lv0. 편지, 모음 제거

정윤숙·2023년 6월 7일
0

TIL

목록 보기
163/192
post-thumbnail

📒 오늘의 공부

1. 프로그래머스

Lv0. 편지

나의 풀이

const solution=(message)=> {
    let answer = 0;
    answer = message.length*2
    return answer;
}

다른 풀이

const solution = (message) => {
    return message.split('').length * 2
}
  • split을 통해 문자를 개별 요소로 분리

Lv0. 모음 제거

나의 풀이

const solution=(my_string)=> {
    const editStr = my_string.replace(/[aeiou]/g, '')
    return editStr
}
  • 처음엔 splice, indexOf를 썼는데 indexOf는 단일 요소 검색만 가능하기 때문에 여러 문자열을 변경할 수 있는 replace 사용

다른 풀이

function solution(my_string) {
    return Array.from(my_string).filter(t => !['a', 'e', 'i', 'o', 'u'].includes(t)).join('');
}
  • includes는 배열 메소드이자 문자열 메소드

    정규식

  • /[aeiou]/gi

    • 'a', 'e', 'i', 'o', 'u' 중 하나의 문자와 일치하는 패턴을 의미
    • g 플래그는 전역 검색 수행
    • i 플래그는 대소문자를 구별하지 않는 검색 수행
profile
프론트엔드 개발자

0개의 댓글