문제
풀이
function solution(s) {
let answer = '';
const numStrArr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const numStr = {
zero: '0',
one: '1',
two: '2',
three: '3',
four: '4',
five: '5',
six: '6',
seven: '7',
eight: '8',
nine: '9',
};
const regex = /zero|one|two|three|four|five|six|seven|eight|nine|[0-9]/g;
const strArr = s.match(regex);
console.log(strArr);
strArr.forEach(str => {
if(numStrArr.includes(str)){
answer = answer + numStr[str];
} else {
answer = answer + str;
}
});
answer = Number(answer);
return answer;
}
function solution(s) {
let numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
var answer = s;
for(let i=0; i< numbers.length; i++) {
let arr = answer.split(numbers[i]);
answer = arr.join(i);
}
return Number(answer);
}
function solution(s) {
s = s.replace(/zero/g, 0)
.replace(/one/g, 1)
.replace(/two/g, 2)
.replace(/three/g, 3)
.replace(/four/g, 4)
.replace(/five/g, 5)
.replace(/six/g, 6)
.replace(/seven/g, 7)
.replace(/eight/g, 8)
.replace(/nine/g, 9)
return parseInt(s);
}