# Basic Algorithm Scripting: Chunky Monkey - code review request

Hi All,

I’m trying to solve the problem as below

``````function chunkArrayInGroups(arr, size) {
// Break it up.
let arr2 = [];
let multiDimensionalArryElementsCount = arr.length / size;
if(arr.length % size != 0){
multiDimensionalArryElementsCount++;
}

let arraySize = 0;

for(let count = 0; count < size; count++){
for(let count2 = 0; count2 < multiDimensionalArryElementsCount; count2++ ){
if( arraySize < arr.length){
arr2[count] = new Array(multiDimensionalArryElementsCount);
arr2[count][count2] = arr[arraySize];
arraySize++;
}else{
break
}
}
}

return arr2;
}

console.log( chunkArrayInGroups(["a", "b", "c", "d"], 2) );
``````

When the result prints out, first element of each of the multi dimensional array is undefined. While debugging I’ve seen value is properly getting inserted at right place.

Please review code, as I’m not getting what’s going wrong

Regards,
Vikram

You re-create the sub-array again, instead of updating it.
This unfortunately means that inside this condition

``````if( arraySize < arr.length){
arr2[count] = new Array(multiDimensionalArryElementsCount);
``````

The statement gets executed and your previous declaration gets overwritten.

Pretty much what’s happening is that:

1- on first iteration you create your sub array

``````[ 'a', <1 empty item> ]
``````

2 - on the second iteration, you assign a new array to it, that changes its value to

``````[ <1 empty item>, 'b' ]
``````

Instead of “adding” `b`.

Hope this helps

Hi,

I figured out that mistake and fixed it as below

``````function chunkArrayInGroups(arr, size) {
// Break it up.
let arr2 = [];
let multiDimensionalArrayElementsCount = Math.floor( arr.length / size );
if(arr.length % size != 0){
multiDimensionalArrayElementsCount++;
}

let arraySize = 0;

for(let count = 0; count < multiDimensionalArrayElementsCount; count++){
arr2[count] = new Array(size);

for(let count2 = 0; count2 < size; count2++ ){
if( arraySize < arr.length){
arr2[count][count2] = arr[arraySize];
arraySize++;
}else{
break
}
}
}

return arr2;
}

console.log( chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) );
``````

But still FCC says below tests fail

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) should return [[0, 1, 2, 3], [4, 5]].

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) should return [[0, 1, 2], [3, 4, 5], [6]].

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) should return [[0, 1, 2, 3], [4, 5, 6, 7], [8]].

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2) should return [[0, 1], [2, 3], [4, 5], [6, 7], [8]].

But when I checked the same code in VS Code it works fine, is FCC complaining as even the last multidimensional element has got longer array size than 1 element ?

Thanks,
Vikram

@Marmiz ok, as expected FCC wants dynamic array size and I fixed it by adding

`size = arr.length - arraySize > size ? size : arr.length - arraySize;`

before initializing multidimensional array size

Do you know any github project where I can contribute to practice concepts learnt in `Javascript Algorithms And Data Structures Certification (300 hours)` ?

Glad you found a way to make it work

Unfortunately I don’t know any open source project that may be suitable.
However if you want to test a bit your skill with data structures you can start building some simple project that involves some public APIs (like weather, your favorite video game statistics…).

There are so many out there for you to use, and you can use those real data and find ways to display them.

For example:

You can use those real data and build anything you want.

1 Like

Thanks for clarification, sorry for late response as well.