Algorithm

[프로그래머스/js] 숫자 문자열과 영단어

죠죠_ 2022. 7. 6. 15:05

문제 :

입력 되는 숫자에 문자열이 있다면 해당 문자열이 의미하는 원래 숫자로 변경하여 return 

 

function solution(s) {
    const answer = 0;
    const dictionary = [
        {key:'zero',value:0},
        {key:'one',value:1},
        {key:'two',value:2},
        {key:'three',value:3},
        {key:'four',value:4},
        {key:'five',value:5},
        {key:'six',value:6},
        {key:'seven',value:7},
        {key:'eight',value:8},
        {key:'nine',value:9},
    ]
    
    const includesChar = /[a-zA-z]/
    if (includesChar.test(s)) {
        dictionary.map(x=> {
           s = s.replace(new RegExp(x.key, 'g'),x.value)
        });
    }

    answer = parseInt(s)
    return answer;
}