Benjamin Semah
Benjamin Semah

Follow

Benjamin Semah

Follow

Chunky Monkey - JavaScript Solution & Walkthrough

(16/16) Learn how to solve coding challenges using FreeCodeCamp's curriculum.

Benjamin Semah's photo
Benjamin Semah
·Apr 16, 2022·

3 min read

Chunky Monkey - JavaScript Solution & Walkthrough
Play this article

Table of contents

  • 16/16 Chunky Monkey
  • Understanding the Challenge
  • Pseudocode
  • Solving the Challenge
  • Final Solution
  • Congratulations!

16/16 Chunky Monkey

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

function chunkArrayInGroups(arr, size) {

  return arr;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);

Credit: FreeCodeCamp.org

Understanding the Challenge

The chunky monkey challenge presents a function that takes two arguments. The first is an array arr. And the second size is a number. The challenge here is to complete the function such that it returns a new array. This new array should contain the same elements as the original array. However, the difference here is that the new array is going to be made of sub-arrays. These sub-arrays should have a length of the given size.

For example, given (["a", "b", "c", "d"], 2), the function should return [ ["a", "b"] , ["c", "d"] ]

Note all the elements "a", "b", "c", "d" are present in both input and output arrays. The only change is that the output array is made up sub-arrays. Each of these sub-arrays has a length of the given size. In this case 2.

Pseudocode

Given an array and a number(size)
  Initialise a variable (newArray) and set it to an empty array
  loop through the input array
  For every iteration
    push a slice of the array into the newArray
    The slice should start at current index (i) and end at (i + size)
  After every iteration
    increase i by the value of size (i + size)
  Return the value of finalArray after the loop is done running

Solving the Challenge

As stated in the pseudocode above, let declare a variable and set its initial value to an empty array.

let finalArray = []

We then loop throught the given array. The loops begins at index 0 and keeps running as long as i < arr.length. Note that the increment after every iteration is i + size.

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

For every iteration of the loop, we push a subArray into the finalArray. This subArray is a slice of the original array. The slice starts at the current value of i and ends at the value at the index of i + size.

This can be a bit tricky. If you're not familiar with the .slice(), I suggest you spend some few minutes reading on it to get a firm grasp of how it works.

return finalArray

Finally, we return finalArray and our function is complete.

Final Solution

function chunkArrayInGroups(arr, size) {
  let finalArray = [];

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

chunkArrayInGroups(["a", "b", "c", "d"], 2); // [["a","b"]["c","d"]]

Congratulations!

You just cracked the 16th challenge in this series.

Cheers and happy coding!

 
Share this