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

ladiolus·2023년 5월 18일
0

programmers

목록 보기
18/25
post-thumbnail

⏱️ 11:01.34

객체로 선언해서 풀려다가 평소에 객체를 잘 안쓰다보니 ...
반복문 돌리는 법을 까먹어서 배열로 선언해서 풀었다. 😅


CODE

function solution(s) {
    const number = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
    
    number.forEach((num, idx) => {
        if(s.includes(num)) s = s.replaceAll(num, idx)
    });
    
    return Number(s);
}

0개의 댓글

Powered by GraphCDN, the GraphQL CDN