DEV Community

loading...
Cover image for Day 3 of JavaScriptmas - Chunky Monkey Solution

Day 3 of JavaScriptmas - Chunky Monkey Solution

Sekti
Keep moving forward. Learn well not learn fast.
・1 min read

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

Discussion (0)