আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি স্ট্রিং এবং একটি সংখ্যা n নেবে (যাতে n স্ট্রিং-এর দৈর্ঘ্যকে সম্পূর্ণ ভাগ করে)। আমাদের দৈর্ঘ্য n-এর স্ট্রিং অ্যারে রিটার্ন করতে হবে যেটে n সংখ্যক সমান অংশ থাকবে।
উদাহরণ
const str = 'this is a sample string'; const num = 5; 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));
আউটপুট
কনসোলে নিচের আউটপুট দেখাবে −
[ 'this ', 'is a ', 'sampl', 'e str', 'ing' ]