DEV Community

Cover image for Day 3 of JavaScriptmas - Chunky Monkey Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on

Day 3 of JavaScriptmas - Chunky Monkey Solution

Day 3 is how to split single array based on the size we provide.

for instance single array with 4 value [a,b,c,d].
We split it into 2 size [[a,b],[c,d]].

so the new array should be has 2 array inside.

There is JavaScript solution


function chunkyMonkey(values, size) {
    let newArray = [];
    let index = 0;

    while(index < values.length) {
        newArray.push(values.slice(index, index += size));
    }

    return newArray;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)