समस्या
हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो संख्या n (n> 0) लेता है। हमारे फ़ंक्शन को एक सरणी लौटानी चाहिए जिसमें विषम या सम अंकों के निरंतर भाग हों। इसका मतलब है कि जब हमें अलग-अलग संख्याएं मिलती हैं (सम के लिए विषम, यहां तक कि विषम के लिए) तो हमें संख्याओं को स्थिति में विभाजित करना चाहिए।
उदाहरण
निम्नलिखित कोड है -
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 ]