http://www.freecodecamp.com/amgranad '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: @amgranad | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20originalLength%20%3D%20arr.length%3B%0A%20%20newArray%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20for(i%3D0%3B%20originalLength%2Fsize%3Ei%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20smallArray%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20for(j%3D0%3B%20size%3Ej%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20smallArray.push(arr%5Bj%5D)%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20if(arr.length%3Csize%20%26%26%20arr.length%3E%200)%7B%0A%20%20%20%20%20%20newArray.push(arr)%3B%0A%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20arr.splice(0%2C%20size)%3B%0A%20%20%20%20%20%20newArray.push(smallArray)%3B%0A%0A%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A%20%20%0A%20%20return%20newArray%3B%0A%7D%0A%0Achunk(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%2C%20%22d%22%5D%2C%202)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function chunk(arr, size) { | |
originalLength = arr.length; | |
newArray = []; | |
for(i=0; originalLength/size>i; i++) | |
{ | |
smallArray = []; | |
for(j=0; size>j; j++) | |
{ | |
smallArray.push(arr[j]); | |
} | |
if(arr.length<size && arr.length> 0){ | |
newArray.push(arr); | |
} else { | |
arr.splice(0, size); | |
newArray.push(smallArray); | |
} | |
} | |
return newArray; | |
} | |
chunk(["a", "b", "c", "d"], 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FFC Bonfire Exercise.