Chunky Monkey code works but seems hacky

Chunky Monkey code works but seems hacky
0

#1

Spoiler: As indicated in title, this code works, so look elsewhere for help on passing.

> `function chunkArrayInGroups(arr, size) {
>   // Break it up.
>   y = [];
>   for (i = 0; i <= (arr.length + 1 / size); i++){
>     x = arr.splice(0, [size]);
>     y.push(x);
>   }
>   if (arr > 0){
>     y.push(arr);
>   }
>   return y;  
> }

> chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);

arr.length + 1 seems to deal with odd integers for size, while the if loop gets me the remaining array. I’m pleased that it works, but is it a workaround? Seems like more testcases would catch my length + 1 setup. Did I miss a more elegant way using the material we’ve covered?


#3

Yep, that’s a lot better.


#4

That is so elegant.
my solution was similar to @Iarflaith

function chunkArrayInGroups(arr, size) {
    // Break it up.

    var myArray = [];
    var subArray = [];

    for (var i = 0; i <= arr.length; i++) {

        subArray = arr.splice(0, size);
        myArray.push(subArray);

    }

    if (arr > 0) {
        myArray.push(arr);
    }

    return myArray;

}

I thought there must be a better way :slight_smile: