I am getting the error of cannot read property ‘0’ of undefined. I have tried googling for the error, and it seems to occur when a program tries to reach an out of bounds index in an array. However, I am not sure which part of my code is causing this.
Your code so far
function chunkArrayInGroups(arr, size) {
// Break it up.
let newArr = [];
let x = 0;
for(let i = 0; i < (arr.length)/size; i++){
for(let j = 0; j < size; j++){
newArr[i][j].push(arr[x]);
x++;
}
}
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36.
It happens because in your second loop, the j might go out of bounds. For example if you have 7 elements in the array, and the size is 3, the third time the index would go beyond the array’s length.
Also as a quick advice, instead of having this: let i = 0; i < (arr.length)/size; i++, you could increment i by the size value, and go up to arr.length
Thanks for the quick response. I agree that this code is unable to capture events ifarr.length % size != 0.
However, I fail to see in this case, if the arguments are ["a", "b", "c", "d"], 2, then arr.length/size = 2 and size = 2 (i.e arr.length % size ==0) why would it still become out of bounds?
I would imagine the last element pushed into newArr would be newArr[1][1] = arr[3].
The issue is that you have declared newArr as an empty array, but in your loop you are trying to push
in int, but you can’t push into something that is undefined.
var newArr = [];
// i = 0 || newArr[0] -> undefined
newArr[i].push([]);
However remember that you can declare variables that are local to each loop iteration.
hope it helps
function chunkArrayInGroups(arr, size) {
// Break it up.
var matrix = [], i, k;
for (i = 0, k = -1; i < arr.length; i++) {
if (i % size === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(arr[i]);
}
return matrix;
}
@freecodecampram@Flopet17 perhaps mark your solutions as spoilers, so that @cornstar94 still have a chance to come with a solution by himself and resort to looking at other people’s code as last chance
p.s. if you want to see a more functional approach that doesn’t involve a for loop
Thanks for taking the time to work through the solution. I think I am beginning to grasp where my error lies.
I assumed that newArr[i][j] exists so long as I pushed a value into it. I am not sure how I got the impression that this would work from the prior exercises.
But I realised that I need to initialise/define the sub-array by performing newArr.push([]).
If I didn’t do that, newArr[i][j] would be undefined as mentioned by @Marmiz.