আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি স্ট্রিং এবং একটি সংখ্যা n নেয় (যেমন n স্ট্রিংয়ের দৈর্ঘ্যকে ঠিকভাবে ভাগ করে) এবং আমাদের স্ট্রিংটির n সমান অংশ সমন্বিত দৈর্ঘ্যের স্ট্রিংয়ের একটি অ্যারে ফেরত দিতে হবে।
উদাহরণ
এর জন্য কোড হবে −
const str = 'we will be splitting this string into parts';
const num = 6;
const divideEqual = (str, num) => {
const len = str.length / num;
const creds = str.split("").reduce((acc, val) => {
let { res, currInd } = acc;
if(!res[currInd] || res[currInd].length < len){
res[currInd] = (res[currInd] || "") + val;
}else{
res[++currInd] = val;
};
return { res, currInd };
}, {
res: [],
currInd: 0
});
return creds.res;
};
console.log(divideEqual(str, num)); আউটপুট
কনসোলে আউটপুট -
[ 'we will ', 'be split', 'ting thi', 's string', ' into pa', 'rts' ]