# FCC Bonfire Series 112: Chunkey Monkey

This time, we’ll be solving Chunkey Monkey! We’ll have to write a function that takes an array and a number. The function must split the array in chunks the length of the second argument, and return them as a multidimensional array.

If the last chunk is smaller than said number, it should return it as is. Here’s a couple examples:

```chunk([0, 1, 2, 3, 4, 5], 2) //-> [[0, 1], [2, 3], [4, 5]]
chunk([6, 7, 8, 9, 4], 3) //-> [[6, 7, 8], [9, 4]]```

As you’ve probably guessed by now, we are going to be using Array methods for this task. Let’s start with some “human code”, we should follow these steps:

1. Loop through the array and add the items to a new temporary array.
2. Everytime we reach the size value provided by the second argument, put the temporary array inside the results array, and create a new nested array.
3. When done, return the result array.

That might sound confusing, but that’s my fault for explaining it like I did, so let’s get to the code so we all understand what’s going on. First, we need to declare three (3) variables:

• Result array: Stores the end result.
• Temporary chunk array: Stores current chunk and gets emptied when a new chunk is created.
• Current chunk counter: It gets incremented as a new chunk is being created, and reset to zero (0) when a new chunk is started.

Here’s what the function will look like when implemented:

```function chunk(array, size) {
var resultArray = [];
var tempArray = [];
var chunk = 0;
for (var i = 0; i < array.length; i++) {
// Add current item to tempArray.
// Increase chunk by 1.
// IS CHUNK EQUAL TO SIZE?
// If it is, add tempArray to resultArray, set chunk to 0 an empty tempArray.
}
return resultArray;
}```

This function will loop through the input array and add the current item to the temporary array. It then increases chunk by one (1). If at this point, chunk is equal to the chunk size specified by the second argument, we should add the temporary array to the results array, and empty the temporary array so that the new chunk will start being created inside it. Obviously, we also reset the chunk variable, since we are starting a new chunk. Finally, we return the array.
That’s all good and pretty, but how do we actually make this happen? We’ll be using the Array method push(). Push adds new items to an array. It can add anything to an array, even another array. A few examples:

```var a = 1,
b = 2,
c = 3;

var arrayOne = [];
arrayOne.push(a); // 
arrayOne.push(b, c); // [1, 2, 3]

var arrayTwo = [5, 6, 7];

var arrayThree = [];

arrayThree.push(arrayOne); // [[1, 2, 3]]
arrayThree.push(arrayTwo); // [[1, 2, 3], [5, 6, 7]]```

Let’s implement this to our function logic:

```function chunk(array, size) {
var resultArray = [];
var tempArray = [];
var chunk = 0;
for (var i = 0; i < array.length; i++) {
tempArray.push(array[i]);
chunk++;
if (chunk == size || i == array.length - 1) {
resultArray.push(tempArray);
chunk = 0;
tempArray = [];
}
}
return resultArray;
}```

That should do the job. The “|| i == array.length – 1” part inside the if statement will add the last chunk to the result array, in case the last chunk is smaller than the set size, it just checks if the current item is the last item in the array.

Now, how about you try creating your own version of the function and make it a bit shorter, maybe.

May the force be with you. And coffee too. Coffee is as important as the force.

Tagged with: