Last active
April 5, 2016 06:18
-
-
Save mmloveaa/998c73509badc7a6c13ff15474812162 to your computer and use it in GitHub Desktop.
4-4 FCC Q10 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
| 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. | |
| Remember to use Read-Search-Ask if you get stuck. Write your own code. | |
| Here are some helpful links: | |
| Array.push() | |
| Array.slice() | |
| function chunkArrayInGroups(arr, size) { | |
| // Break it up. | |
| var arr1=[]; | |
| for(var i=0; i<arr.length; i+=size) { | |
| // console.log(arr.slice(i,size)) | |
| arr1.push(arr.slice(i,i+size)) | |
| } | |
| return arr1; | |
| } | |
| chunkArrayInGroups(["a", "b", "c", "d"], 2); | |
| chunkArrayInGroups(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]]. | |
| chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) should return [[0, 1, 2], [3, 4, 5]]. | |
| chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) should return [[0, 1], [2, 3], [4, 5]]. | |
| chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) should return [[0, 1, 2, 3], [4, 5]]. | |
| chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) should return [[0, 1, 2], [3, 4, 5], [6]]. | |
| chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) should return [[0, 1, 2, 3], [4, 5, 6, 7], [8]]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment