समस्या
हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक संख्या 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 ]