Created
December 10, 2015 21:14
-
-
Save anonymous/c97e0e96d4b06a164919 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/goteamtim 's solution for Bonfire: Chunky Monkey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bonfire: Chunky Monkey | |
| // Author: @goteamtim | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function chunk(arr, size) { | |
| var chunkedArray = []; | |
| var count = 0; | |
| var newArray = []; | |
| for(var i = 0; i < arr.length; i++){ | |
| //If the max number is reached for the smaller arrays. 0 everything out. | |
| if(count === size){ | |
| count = 0; | |
| newArray = []; | |
| newArray.push(arr[i]); | |
| //If the size is less than what it should be, continue to add to the array | |
| }else if(count <= size){ | |
| newArray.push(arr[i]); | |
| count++; | |
| } | |
| //Once the Array is the correct size push it to the main array or if its the last number in the main array | |
| if(newArray.length === size | i === (arr.length-1)){ | |
| chunkedArray.push(newArray); | |
| newArray = []; | |
| count = 0; | |
| } | |
| } | |
| return chunkedArray; | |
| } | |
| chunk(["a", "b", "c", "d"], 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment