সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা 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' ]