I need another set of eyes. There has to be something small I'm just missing here.
I'm inputting a string which is brokend into an array of words, then takes every 5 indexes (words) and joins them into their own index.
This is accomplished through a recursive call.
The final call of the recursion prints the correct result I'm looking for while inside, but the actual returned value outside is undefined..
Please help me out here.
const limit = str => {
const arr = str.split(' ');
function recursiveLimit(arr, acc = []) {
if (arr.length !== 0) {
const toSlice = arr.length < 5 ? arr.length : 5;
const newArr = arr.slice(toSlice);
const newAcc = [...acc, arr.slice(0, toSlice).join(' ')];
recursiveLimit(newArr, newAcc);
} else {
console.log('final array: ', acc); // the array I want to return (looks good here)
return acc; // return it
}
}
return recursiveLimit(arr); // undefined
};
console.log(
'OUTPUT: limit',
limit(
'one two three four five six seven eight nine ten eleven twelve thirteen fourteen'
)
);
ifbranch, there's noreturnstatement.reduce()?