Algorithm Chunky Monkey - thelastmile/FreeCodeCamp GitHub Wiki
Algorithm Chunky Monkey

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
:checkered_flag: Problem Explanation:
Our goal for this Algorithm is to split arr (first argument) into smaller chunks of arrays with the length provided by size (second argument). There are 4 green checks (objectives) our code needs to pass in order to complete this Algorithm:
(['a', 'b', 'c', 'd'], 2)is expected to be['a', 'b'], ['c', 'd'](/thelastmile/FreeCodeCamp/wiki/'a',-'b'],-['c',-'d')([0, 1, 2, 3, 4, 5], 3)is expected to be[0, 1, 2], [3, 4, 5](/thelastmile/FreeCodeCamp/wiki/0,-1,-2],-[3,-4,-5)([0, 1, 2, 3, 4, 5], 2)is expected to be[0, 1], [2, 3], [4, 5](/thelastmile/FreeCodeCamp/wiki/0,-1],-[2,-3],-[4,-5)([0, 1, 2, 3, 4, 5], 4)is expected to be[0, 1, 2, 3], [4, 5](/thelastmile/FreeCodeCamp/wiki/0,-1,-2,-3],-[4,-5)
Relevant Links
:speech_balloon: Hint: 1
The links above suggest to use Array.push(), so let's start by first creating a new array to store the smaller arrays we will soon have like this:
var newArray = [];
try to solve the problem now
:speech_balloon: Hint: 2
Next we'll need a for loop to loop through arr.
try to solve the problem now
:speech_balloon: Hint: 3
Finally, we need a method to do the actual splitting and we can use Array.slice() to do that. The key to this Algorithm is understanding how a for loop, size, Array.slice() and Array.push() all work together.
try to solve the problem now
Spoiler Alert!
![]()
Solution ahead!
:beginner: Basic Code Solution:
function chunkArrayInGroups(arr, size) {
var temp = [];
var result = [];
for (var a = 0; a < arr.length; a++) {
if (a % size !== size - 1)
temp.push(arr[a]);
else {
temp.push(arr[a]);
result.push(temp);
temp = [];
}
}
if (temp.length !== 0)
result.push(temp);
return result;
}
:rocket: Run Code
Code Explanation:
- Firstly, we create two empty arrays called
tempandresult, which we will eventually return. - Our for loop loops until
ais equal to or more than the length of the array in our test. - Inside our loop, we push to
tempusingtemp.push(arr[a]);if the remainder ofa / sizeis not equal tosize - 1. - Otherwise, we push to
temp, pushtempto theresultvariable and resettempto an empty array. - Next, if
tempisn't an empty array, we push it toresult. - Finally, we return the value of
result.
Relevant Links
:sunflower: Intermediate Code Solution:
function chunkArrayInGroups(arr, size) {
// Break it up
// It's already broken :(
arr = arr.slice();
var arr2 = [];
for(var i = 0, len = arr.length; i < len; i+=size) {
arr2.push(arr.slice(0, size));
arr = arr.slice(size);
}
return arr2;
}
:rocket: Run Code
Code Explanation:
- Firstly, we slice
arrusingarr.slice()and create an empty array calledarr2. - Our for loop loops until
iis equal to or more than the length of the array in our test. We also addsizeontoieach time we loop. - Inside our loop, we push to
arr2usingarr.slice(0, size). - After pushing to
arr2, we letarrequal toarr.slice(size). - Finally, we return the value of
arr2.
Relevant Links
:rotating_light: Advanced Code Solution:
function chunkArrayInGroups(arr, size) {
// Break it up.
var newArr = [];
var i = 0;
while (i < arr.length) {
newArr.push(arr.slice(i, i+size));
i += size;
}
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
:rocket: Run Code
Code Explanation:
- Firstly, we create two variables.
newArris an empty array which we will push to. We also have theivariable set to zero, for use in our while loop. - Our while loop loops until
iis equal to or more than the length of the array in our test. - Inside our loop, we push to the
newArrarray usingarr.slice(i, i+size). For the first time it loops, it will look something like:
newArr.push(arr.slice(1, 1+2))
- After we push to
newArr, we add the variable ofsizeontoi. - Finally, we return the value of
newArr.
Relevant Links
:trophy: Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat:
Thanks @kirah1314 @Rafase282 @jsommamtek @oshliaer for your help with Algorithm: Chunky Monkey
:clipboard: NOTES FOR CONTRIBUTIONS:
- :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See :point_right:
Wiki Challenge Solution Templatefor reference.