# Coding Challenge - #12: Chunk An Array

**URL:** <https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192>\
**Category:** web dev\
**Created:** [August 29, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192 "2026-08-29T22:00:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [August 29, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192/1 "2026-08-29T22:00:06Z")

</div>

Write a function `chunk(arr, size)` that splits an array into smaller sub-arrays of a specified length. 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:**

- Must return a new array of chunked arrays.
- The final chunk can contain fewer elements if the input array cannot be divided evenly.
- Do not use any external libraries.

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

---

<div class="post-metadata">

**Author:** ![Apexcodes](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/apexcodes/32/33967_2.png) [@Apexcodes](https://forum.kirupa.com/u/Apexcodes)\
**Post date:** [August 30, 2026, 4:23am UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192/2 "2026-08-30T04:23:36Z")

</div>

A simple approach is to step through the array by the chunk size and use `slice()`:

```auto
function chunk(arr, size) {
  const result = [];

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

  return result;
}

```

For example:

```auto
chunk([1, 2, 3, 4, 5], 2);
// [[1, 2], [3, 4], [5]]

```

This keeps the original array unchanged and naturally handles the final smaller chunk.

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [August 30, 2026, 4:40am UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192/3 "2026-08-30T04:40:04Z")

</div>

nice, slicing is definitely a good way to approach this. we’ll see how close that gets you later today!

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [August 30, 2026, 6:00am UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192/4 "2026-08-30T06:00:20Z")

</div>

Yeah `slice()` is definitely the move here. way cleaner than trying to loop through and manage indices manually.

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [August 30, 2026, 11:00pm UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192/5 "2026-08-30T23:00:16Z")

</div>

**Challenge solution:** The task requires splitting an array into sub-arrays of a given size, handling cases where the last sub-array might be smaller.

**One way to do it:**

```js
function chunk(arr, size) {
  const chunkedArr = [];
  let index = 0;

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

  return chunkedArr;
}

```

**Why:**  
This solution iterates through the input array using a while loop and an index. In each iteration, it uses the slice method to extract a sub-array of the specified ‘size’ and pushes it into the ‘chunkedArr’. The index is then incremented by ‘size’ to move to the next chunk’s starting point, ensuring all elements are processed and the last chunk correctly handles remaining elements.

* * *

**Got it:** @Apexcodes 🏆

**First-answer leaderboard**

1. @kirupa - 6 (firsts) 🏆
2. @Apexcodes - 5 (firsts)
3. @adnanahmed - 2 (firsts)
4. @emmawalter5 - 2 (firsts)

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [August 31, 2026, 8:00am UTC](https://forum.kirupa.com/t/coding-challenge-12-chunk-an-array/683192/6 "2026-08-31T08:00:26Z")

</div>

This is a perfectly functional approach. My main concern with `slice` in a loop like this is often performance on very large arrays, though for typical challenge sizes it’s negligible. I’ve seen some solutions use `reduce` to build up the chunks, which can be a bit more declarative, but often less immediately clear than a `while` loop with an explicit index. Confidence: high
