My Chunky monkey function works on a size that divides evenly into the array length. However if I a value that does not divide evenly I recive this error in the console: "TypeError: Cannot read property ‘push’ of undefined"
Can some one please help me figure this out?
Here is my code:
function chunkArrayInGroups(arr, size) {
var nArr =[[],[]];
var k = 0;
var nArrRows;
/**
*
* if size divides evenly into the length of arr
* then set the number of rows to the dividend of the two
* else round the dividend up by 1 and set the number of rows to the result.
*/
if (arr.length%size === 0) {
nArrRows = arr.length/size;
} else {
nArrRows = Math.ceil(arr.length/size);
}
//nested loop to populate the array
for (var i = 0; i < nArrRows; i++) {
for (var j = 0; j < size; j++) {
if(arr[k] !== null){
nArr[i].push(arr[k]);
k++;
}
//k++;
}
}
return nArr;
}