I am struggling with the Chunk Monkey challenge (in the Basic Algorithm Scripting section).
My code is:
function chunkArrayInGroups(arr, size) {
// Break it up.
var newArray = [];
var elementArray = [];
var j;
for (var i=0; i<arr.length; i++){
j = i % size;
elementArray[j] = arr[i];
if (j==(size-1) || i==(arr.length-1)){
newArray.push(elementArray);
}
}
return newArray;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
I don’t know what wrong with that but result is [ [“c”,“d”],[“c”, “d”] while the right answer should be [ [“a”,“b”],[“c”,“d”]]. Could anyone help me. Thank you very much!