문제
숫자 n(n>0)을 취하는 JavaScript 함수를 작성해야 합니다. 우리의 함수는 홀수 또는 짝수의 연속 부분을 포함하는 배열을 반환해야 합니다. 즉, 다른 숫자(짝수는 홀수, 홀수는 짝수)를 만날 때 위치에서 숫자를 분할해야 합니다.
예
다음은 코드입니다 -
const num = 124579;
const splitDifferent = (num = 1) => {
const str = String(num);
const res = [];
let temp = '';
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!temp || +temp[temp.length - 1] % 2 === +el % 2){
temp += el;
}else{
res.push(+temp);
temp = el;
};
};
if(temp){
res.push(+temp);
temp = '';
};
return res;
};
console.log(splitDifferent(num)); 출력
[ 1, 24, 579 ]