코딩테스트 연습 - 숫자 문자열과 영단어 | 프로그래머스 (programmers.co.kr)
접근 : 구현
1. numbers에 문자열과 index번호로 정렬
2. numbers를 forEach로 돌면서 모든 문자열을 숫자로 변경
3. indexOf()와 splice로 치환
Javascript
function solution(s) {
let answer = "";
let numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"];
numbers.forEach((num,idx)=>{
while(s.indexOf(num)!==-1){
let indx = s.indexOf(num);
s=s.split("");
s.splice(indx,num.length,idx);
s= s.join("");
}
})
answer = Number(s);
return answer;
}
'코딩테스트(알고리즘) > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 실패율 (Javascript) (0) | 2022.06.24 |
---|---|
[프로그래머스] 크레인 인형뽑기 게임 (Javascript) (0) | 2022.06.23 |
[프로그래머스] 신고 결과 받기 (Javascript) (0) | 2022.06.23 |
[프로그래머스] 짝지어 제거하기 (Javascript) (0) | 2022.06.22 |
[프로그래머스] 124 나라 (Javascript) (0) | 2022.06.22 |