2018-10-04 14:37:37 +01:00
---
id: a9bd25c716030ec90084d8a1
title: Chunky Monkey
isRequired: true
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16005
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
Write a function that splits an array (first argument) into groups the length of < code > size< / code > (second argument) and returns them as a two-dimensional array.
2019-11-19 19:54:48 -05:00
Remember to use < a href = "https://www.freecodecamp.org/forum/t/how-to-get-help-when-you-are-stuck-coding/19514" target = "_blank" > Read-Search-Ask< / a > if you get stuck. Write your own code.
2018-10-04 14:37:37 +01:00
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups(["a", "b", "c", "d"], 2)</ code > should return < code > [["a", "b"], ["c", "d"]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups(["a", "b", "c", "d"], 2), [["a", "b"], ["c", "d"]]);
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)</ code > should return < code > [[0, 1, 2], [3, 4, 5]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3), [[0, 1, 2], [3, 4, 5]]);
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)</ code > should return < code > [[0, 1], [2, 3], [4, 5]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2), [[0, 1], [2, 3], [4, 5]]);
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)</ code > should return < code > [[0, 1, 2, 3], [4, 5]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4), [[0, 1, 2, 3], [4, 5]]);
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)</ code > should return < code > [[0, 1, 2], [3, 4, 5], [6]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3), [[0, 1, 2], [3, 4, 5], [6]]);
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)</ code > should return < code > [[0, 1, 2, 3], [4, 5, 6, 7], [8]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4), [[0, 1, 2, 3], [4, 5, 6, 7], [8]]);
2018-10-20 21:02:47 +03:00
- text: < code > chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)</ code > should return < code > [[0, 1], [2, 3], [4, 5], [6, 7], [8]]</ code > .
2019-07-24 01:47:32 -07:00
testString: assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2), [[0, 1], [2, 3], [4, 5], [6, 7], [8]]);
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function chunkArrayInGroups(arr, size) {
// Break it up.
return arr;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function chunkArrayInGroups(arr, size) {
let out = [];
for (let i = 0; i < arr.length ; i + = size ) {
out.push(arr.slice(i, i + size));
}
return out;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
```
< / section >