I am expected to write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array. Therefore, chunkArrayInGroups(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]]
function chunkArrayInGroups(arr, size) {
// Break it up.
var result=[];
for (var i=0;i<=Math.ceil(arr.length/size);i++){
var j=0;
if(i!== 0){
j+=size;
size+=size;
}
result[i] = arr.slice(j , size);
}
//return arr.length/size
//results in 1.5
return result;
// results [["a", "b"], ["c", "d"]]
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
I am getting the desired result but I'm not very satisfied with my solution, also, more importantly "if" arr.length = 4; and, size =2; then why is arr.length/size = 1 ? shouldn't it be 4/2=2?