Hi,
I just solved the Chunkey Monkey Challenge and I’d like some feedback. Something tells me I could have solved this more efficiently. I feel like there was probably a way I could have modified the array in place.
My Solution
function chunkArrayInGroups(arr, size) {
var newArr = [];
var count = 0;
while (count < arr.length){
newArr.push(arr.slice(count,(size + count)));
count += size;
}
return newArr;
}
What I wanted to do, in English, was “Take the given array, remove x number of items from the array and place them in a new array”. Instead of removing them, my code is copying them then starting the slice after the given count for them.
Is there a way to move items from one array to another rather than to copy?
Another, less elegant solution I thought of would to have a for loop that ran the shift()
method the number of times given. Unfortunately, shift()
doesn’t take any params.