[알고리즘] 숫자 문자열과 영단어

조예진·2022년 7월 3일
2
post-thumbnail

Lv1. 숫자 문자열과 영단어 (by JS)

왜 replaceAll이 안 먹는 것인가...에 대해서만 생각났던 알고리즘 문제^^
그래도 푸는 시간 별로 안 걸려서 알고리즘 자신감 붙게 해줬던 고마운 문제!!

코드

function solution(s) {
    let answer = s;
    const list = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

        list.map((item, idx) => {
            // 해당 문자열 조건
            if(s.indexOf(item) !== -1) {
              //replaceAll 역할
              s.split(item).forEach(() => answer = answer.replace(item, idx))
            }
        })
    return parseInt(answer)
}
profile
블로그 이전 중 -> https://devjooj.tistory.com/

1개의 댓글

comment-user-thumbnail
2022년 7월 12일

와 도움이 너무나도 잘된거같습니다

답글 달기