undefined
[프로그래머스] 숫자 문자열과 영단어 -JS 본문
반응형
문제 설명
문제 풀이
function solution(s) {
const arr = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
let answer = []
let word = ""
const splited = s.split("")
splited.map((e,i) => {
if(!isNaN(e)) {
answer.push(e)
} else {
word += e
if(arr.includes(word)) {
answer.push(arr.indexOf(word))
word = ""
}
}
})
return +answer.join("")
}
1. arr 배열 만들어 준다 (index값 = 요소의값)
2. s를 split으로 배열로 나누어주고 mapping
3. 요소가 숫자일 경우 먼저 걸러줌
4. 문자라면 각각 차례로 더해줬을때 만약 arr에 word값이 포함된다면 arr에서의 word의 index값을 넣어줌
5. word 초기화
6. join으로 하나로 묶어 배열을 풀고 상수화
개선 사항
반응형
'Coding Test' 카테고리의 다른 글
[프로그래머스] 체육복 -JS (0) | 2022.06.19 |
---|---|
[프로그래머스] 모의고사 -JS (0) | 2022.06.11 |
[프로그래머스] 3진법 뒤집기 -JS (0) | 2022.06.09 |
[프로그래머스] 두 개 뽑아서 더하기 -JS (0) | 2022.06.09 |
[프로그래머스] 최대공약수와 최소공배수 -JS (0) | 2022.06.08 |
Comments