समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो String क्लास के प्रोटोटाइप ऑब्जेक्ट पर रहता है। इसे एकमात्र आर्गुमेंट के रूप में एक स्ट्रिंग सेपरेटर लेना चाहिए (हालांकि मूल split फ़ंक्शन दो आर्गुमेंट लेता है)। और हमारा फ़ंक्शन सेपरेटर द्वारा अलग किए गए स्ट्रिंग के भागों का एक ऐरे लौटाना चाहिए।
उदाहरण
निम्नलिखित कोड है −
const str = 'this is some string'; String.prototype.customSplit = (sep = '') => { const res = []; let temp = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(el === sep || sep === '' && temp){ res.push(temp); temp = ''; }; if(el !== sep){ temp += el; } }; if(temp){ res.push(temp); temp = ''; }; return res; }; console.log(str.customSplit(' '));आउटपुट
[ 'this', 'is', 'some', 'string' ]