|
| 1 | +--- |
| 2 | +id: a9bd25c716030ec90084d8a1 |
| 3 | +title: Implement the Chunky Monkey Algorithm |
| 4 | +challengeType: 26 |
| 5 | +dashedName: implement-the-chunky-monkey-algorithm |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +Fulfill the user stories below and get all the tests to pass to complete the lab. |
| 11 | + |
| 12 | +**User Stories:** |
| 13 | + |
| 14 | +1. Write a function named `chunkArrayInGroups` that takes an array as first argument and a number as second argument. The function should split the array into smaller arrays of length equal to the second argument and returns them as a two-dimensional array. |
| 15 | + |
| 16 | +# --hints-- |
| 17 | + |
| 18 | +`chunkArrayInGroups(["a", "b", "c", "d"], 2)` should return `[["a", "b"], ["c", "d"]]`. |
| 19 | + |
| 20 | +```js |
| 21 | +assert.deepEqual(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2), [ |
| 22 | + ['a', 'b'], |
| 23 | + ['c', 'd'] |
| 24 | +]); |
| 25 | +``` |
| 26 | + |
| 27 | +`chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)` should return `[[0, 1, 2], [3, 4, 5]]`. |
| 28 | + |
| 29 | +```js |
| 30 | +assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3), [ |
| 31 | + [0, 1, 2], |
| 32 | + [3, 4, 5] |
| 33 | +]); |
| 34 | +``` |
| 35 | + |
| 36 | +`chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)` should return `[[0, 1], [2, 3], [4, 5]]`. |
| 37 | + |
| 38 | +```js |
| 39 | +assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2), [ |
| 40 | + [0, 1], |
| 41 | + [2, 3], |
| 42 | + [4, 5] |
| 43 | +]); |
| 44 | +``` |
| 45 | + |
| 46 | +`chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)` should return `[[0, 1, 2, 3], [4, 5]]`. |
| 47 | + |
| 48 | +```js |
| 49 | +assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4), [ |
| 50 | + [0, 1, 2, 3], |
| 51 | + [4, 5] |
| 52 | +]); |
| 53 | +``` |
| 54 | + |
| 55 | +`chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)` should return `[[0, 1, 2], [3, 4, 5], [6]]`. |
| 56 | + |
| 57 | +```js |
| 58 | +assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3), [ |
| 59 | + [0, 1, 2], |
| 60 | + [3, 4, 5], |
| 61 | + [6] |
| 62 | +]); |
| 63 | +``` |
| 64 | + |
| 65 | +`chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)` should return `[[0, 1, 2, 3], [4, 5, 6, 7], [8]]`. |
| 66 | + |
| 67 | +```js |
| 68 | +assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4), [ |
| 69 | + [0, 1, 2, 3], |
| 70 | + [4, 5, 6, 7], |
| 71 | + [8] |
| 72 | +]); |
| 73 | +``` |
| 74 | + |
| 75 | +`chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)` should return `[[0, 1], [2, 3], [4, 5], [6, 7], [8]]`. |
| 76 | + |
| 77 | +```js |
| 78 | +assert.deepEqual(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2), [ |
| 79 | + [0, 1], |
| 80 | + [2, 3], |
| 81 | + [4, 5], |
| 82 | + [6, 7], |
| 83 | + [8] |
| 84 | +]); |
| 85 | +``` |
| 86 | + |
| 87 | +# --seed-- |
| 88 | + |
| 89 | +## --seed-contents-- |
| 90 | + |
| 91 | +```js |
| 92 | +``` |
| 93 | + |
| 94 | +# --solutions-- |
| 95 | + |
| 96 | +```js |
| 97 | +function chunkArrayInGroups(arr, size) { |
| 98 | + let out = []; |
| 99 | + |
| 100 | + for (let i = 0; i < arr.length; i += size) { |
| 101 | + out.push(arr.slice(i, i + size)); |
| 102 | + } |
| 103 | + |
| 104 | + return out; |
| 105 | +} |
| 106 | + |
| 107 | +chunkArrayInGroups(['a', 'b', 'c', 'd'], 2); |
| 108 | +``` |
0 commit comments