# Implement the Chunky Monkey Algorithm - Implement the Chunky Monkey Algorithm

**URL:** <https://forum.freecodecamp.org/t/implement-the-chunky-monkey-algorithm-implement-the-chunky-monkey-algorithm/749873>\
**Category:** I Built This\
**Created:** [June 6, 2025, 2:12am UTC](https://forum.freecodecamp.org/t/implement-the-chunky-monkey-algorithm-implement-the-chunky-monkey-algorithm/749873 "2025-06-06T02:12:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fish\_99](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fish_99/32/389824_2.png) [@fish\_99](https://forum.freecodecamp.org/u/fish_99)\
**Post date:** [June 6, 2025, 2:12am UTC](https://forum.freecodecamp.org/t/implement-the-chunky-monkey-algorithm-implement-the-chunky-monkey-algorithm/749873/1 "2025-06-06T02:12:46Z")

</div>

### Tell us what’s happening:

I finally completed the tests, does anyone have any comments or recommendations on my code?

### Your code so far

```js
function chunkArrayInGroups(array, n) {
  const arraysWithSamelength = Math.floor(array.length / n);
  let splitCount = arraysWithSamelength;
  if (array.length % n != false) splitCount++ ;
  const newArray = [];
  for (let i = 1; i <= splitCount; i++) {
    if (array.length > n) {
      newArray.push(array.splice(0, n))
    } else {
      newArray.push(array)
    }
  }
  return newArray
}

console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4))

```

### Your browser information:

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36`

### Challenge Information:

Implement the Chunky Monkey Algorithm - Implement the Chunky Monkey Algorithm  
[https://www.freecodecamp.org/learn/full-stack-developer/lab-chunky-monkey/implement-the-chunky-monkey-algorithm](https://www.freecodecamp.org/learn/full-stack-developer/lab-chunky-monkey/implement-the-chunky-monkey-algorithm)

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [June 6, 2025, 5:59am UTC](https://forum.freecodecamp.org/t/implement-the-chunky-monkey-algorithm-implement-the-chunky-monkey-algorithm/749873/2 "2025-06-06T05:59:51Z")

</div>

Few notes:

- `arraysWithSamelength` is defined just to reassign it in the next line to `splitCount`.
- The `array.length % n != false` condition works because `!=` operator tries to convert both sides to the same type. `array.length % n` could be instead strictly compared to the actual integer, to more explicitly show condition. Otherwise, since condition in `if` will be considered in a boolean context, it means that `array.length % n` alone would be considered to be either truthy or falsy, comparing it to with `!= false` is like saying: _check if it’s not falsy_. This is what `if (condition)` does on it own.
- `splitCount` appears like a helpful number. After all it’s good to know how many times to loop in the `for` loop. Is it really needed though? There’s a bit _troubles_ to calculate it , one value is _floored_ with `Math.floor`, then sometimes result is incremented, sometimes not. What happens inside of the loop can give some idea how it could be handled without the need for `splitCount`.

---

<div class="post-metadata">

**Author:** ![smit\_007](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/smit_007/32/396465_2.png) [@smit\_007](https://forum.freecodecamp.org/u/smit_007)\
**Post date:** [June 6, 2025, 2:15pm UTC](https://forum.freecodecamp.org/t/implement-the-chunky-monkey-algorithm-implement-the-chunky-monkey-algorithm/749873/3 "2025-06-06T14:15:31Z")

</div>

Good work on passing all the tests! There are few steps in which way you can clean the code and make it more efficient.

1. You’re using `splice()`, which modifies the original array. This isn’t the best way, as it’s better to use `slice()` instead, as it doesn’t modifies the original array.

2. There’s no need to compute the number of chunks before, as it makes the code more complex. You can simplify the logic by looping through the array and incrementing by `n`.

3. The condition `array.length % n != false` is confusing because it mixes types, it’s more clearer and simpler to check directly if the remainder is not zero.

Let us know if this helps, good luck!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [December 6, 2025, 2:16am UTC](https://forum.freecodecamp.org/t/implement-the-chunky-monkey-algorithm-implement-the-chunky-monkey-algorithm/749873/4 "2025-12-06T02:16:19Z")

</div>

Closed on December 6, 2025, 2:16am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
