हमें एक जावास्क्रिप्ट फंक्शन लिखना है जो एक स्ट्रिंग और एक नंबर n ले (ऐसा कि 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' ]