I need to slice and array using a for loop and the slice() and push() methods.
I tried every possible way (except the right one obviously) but no way to figure out the solution.
Please to explain because I am always in trouble when it comes to recursion and for loops.
function chunkArrayInGroups(arr, size) {
// Break it up.
var finalArray = [];
// var firsPart = arr.slice(0, 2);
// var secondPart = arr.slice(2, 4);
// console.log(firsPart);
// console.log(secondPart);
// finalArray.push(firsPart, secondPart);
for (var i = 0; i < arr.length; i++) {
var slicingIndex = 0;
var sliced = arr.slice(size);
finalArray.push(sliced);
}
return finalArray;
}
console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4));
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));
Thanks in advance for your help!