-
-
Save anonymous/8fe64ac59726a2749709 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/v3rse '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: @v3rse | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20%2F%2F%20Break%20it%20up.%0A%20%20%2F%2Fcreate%20return%20array%0A%20%20var%20rArray%20%3D%20%5B%5D%3B%0A%20%20%0A%20%20var%20tempArray%20%3D%20%5B%5D%3B%0A%20%20%2F%2Floop%20through%0A%20%20for(var%20i%20%3D%200%3B%20i%20%3C%3D%20arr.length%20%3B%20i%2B%2B)%7B%0A%20%20%20%20console.log(i)%3B%0A%20%20%20%20if(tempArray.length%20%3D%3D%20size%20%7C%7C%20i%20%3D%3D%20arr.length)%7B%0A%20%20%20%20%20%20%2F%2Fif%20full%0A%20%20%20%20%20%20rArray.push(tempArray)%3B%0A%20%20%20%20%20%20%2F%2Fempty%20temp%0A%20%20%20%20%20%20tempArray%20%3D%20%5B%5D%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20tempArray.push(arr%5Bi%5D)%3B%0A%20%20%20%20%7Delse%7B%0A%20%20%20%20%20%20tempArray.push(arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%20return%20rArray%3B%0A%7D%0A%0Achunk(%5B0%2C%201%2C%202%2C%203%2C%204%2C%205%5D%2C%204)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function chunk(arr, size) { | |
// Break it up. | |
//create return array | |
var rArray = []; | |
var tempArray = []; | |
//loop through | |
for(var i = 0; i <= arr.length ; i++){ | |
console.log(i); | |
if(tempArray.length == size || i == arr.length){ | |
//if full | |
rArray.push(tempArray); | |
//empty temp | |
tempArray = []; | |
tempArray.push(arr[i]); | |
}else{ | |
tempArray.push(arr[i]); | |
} | |
} | |
return rArray; | |
} | |
chunk([0, 1, 2, 3, 4, 5], 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment