Hello, i need help I’m looking a solution following what i already did with my code. I tried to figured it out but i have a problem with this code. Some thing don’t work with my array.
I don’t know if it’s because i’m getting tired. I should maybe sleep on it and look at it tomorrow with a fresher mind. Maybe not a good idea to seek help
/*
Link: https://beta.freecodecamp.org/en/challenges/basic-algorithm-scripting/chunky-monkey
*/
function chunkArrayInGroups( arr, size ) {
let newArr = [];
var size2 = size;
let arrL = arr.length;
let calc = arrL / size;
let x = 0;
let z = 0;
for( let k = size2; k <= arrL; k += size2 ) {
newArr[z] = arr.slice(x, k);
z = z + size2;
x = x + size2;
}
let newArr0L = newArr[0].length;
if( newArr0L > arrL ) {
newArr[newArr0L] = arr.slice(newArr0L, arrL );
}
//I have empty but i don't know why ...
/* newArr = newArr.filter(function(e) {
// return String(e).trim();
}); */
return newArr;
};
// console.log( chunkArrayInGroups(["a", "b", "c", "d"], 2) ); //[["a", "b"], ["c", "d"]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) ); //[[0, 1, 2], [3, 4, 5]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) ); //[[0, 1], [2, 3], [4, 5]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) ); //[[0, 1, 2, 3], [4, 5]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) ); //[[0, 1, 2], [3, 4, 5], [6]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) ); //[[0, 1, 2, 3], [4, 5, 6, 7], [8]]
// console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2) ); //[[0, 1], [2, 3], [4, 5], [6, 7], [8]]