# Coding Challenge - #30: Chunk Array Elements

**URL:** <https://forum.kirupa.com/t/coding-challenge-30-chunk-array-elements/683659>\
**Category:** web dev\
**Created:** [October 10, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-30-chunk-array-elements/683659 "2026-10-10T22:00:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [October 10, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-30-chunk-array-elements/683659/1 "2026-10-10T22:00:07Z")

</div>

Write a function `chunk(arr, size)` that splits an array into groups of length `size` and returns them as a two-dimensional array. If the array cannot be split evenly, the final chunk should contain the remaining elements. For example, `chunk([1, 2, 3, 4, 5], 2)` should return `[[1, 2], [3, 4], [5]]`.

```js
function chunk(arr, size) {
  // your code here
}

```

**Rules:**

- Return an empty array if the input array is empty.
- Assume size is always an integer greater than 0.
- Do not mutate the original input array.

Post your solution as a reply. Answer goes up in about a day.

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [October 11, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-30-chunk-array-elements/683659/2 "2026-10-11T22:00:17Z")

</div>

This is a classic problem. I remember a similar one from a data visualization project where I needed to group data points for a small multiples chart.

```auto
function chunk(arr, size) {
  if (arr.length === 0) {
    return [];
  }

  const result = [];
  for (let i = 0; i < arr.length; i += size) {
    result.push(arr.slice(i, i + size));
  }
  return result;
}

```
