[JS] 숫자 문자열과 영단어

 

프로그래머스

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

programmers.co.kr

 

문자로 작성된 글자가 있다면 모두 숫자로 바꿔야하는 조건으로, 

words 배열을 돌면서 해당 문자가 s 문자열에 있다면, 모두 숫자로 바꾼다

function solution(s) {
    words = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    
    for(let i=0; i<words.length; i++){
        if(s.includes(words[i])) s = s.replaceAll(words[i], i);
    }
    
    return Number(s);
}