min
프로그래머스 숫자 문자열과 영단어 본문
<문제>
https://school.programmers.co.kr/learn/courses/30/lessons/81301
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
<나의 풀이>
function solution(s) {
const arr = ["zero","one","two","three","four","five","six","seven","eight","nine"]
for(let i = 0; i< arr.length;i++) {
s = s.replaceAll(arr[i],i)
}
return Number(s);
}
<다른 사람의 풀이>
const solution = (s) => {
const wordsMap = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};
let words = Object.keys(wordsMap);
words.forEach((e, i) => {
while (s.includes(e)) {
s = s.replace(e, wordsMap[e]);
}
});
return +s;
};
<궁금중>
replace All
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
String.prototype.replaceAll() - JavaScript | MDN
replaceAll() 메서드는 pattern의 모든 일치 항목이 replacement로 대체된 새 문자열을 반환합니다. pattern은 문자열 또는 RegExp일 수 있으며 replacement는 각 일치 항목에 대해 호출되는 문자열 또는 함수일
developer.mozilla.org
'알고리즘' 카테고리의 다른 글
프로그래머스 약수의 개수와 덧셈 자바스크립트 (0) | 2023.06.20 |
---|---|
프로그래머스 신규 아이디 추천 자바스크립트 (0) | 2023.06.19 |
프로그래머스 모의고사 자바스크립트 (0) | 2023.06.19 |
프로그래머스 시저암호 자바스크립트 (0) | 2023.06.19 |
프로그래머스 자릿수 더하기 자바스크립트 (0) | 2023.06.18 |