Forked from anonymous/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20var%20i%3D0%3B%0A%20%20var%20edArr%20%3D%20%5B%5D%3B%0A%20%20while%20(arr.length%3E0)%20%7B%0A%20%20%20%20var%20sub%20%3D%20%5B%5D%3B%0A%20%20%20%20var%20number%20%3D%20(arr
Created
November 30, 2015 23:04
-
-
Save eridbardhaj/7ec5ceb4be78dd8e70aa to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/eridbardhaj 's solution for Bonfire: Chunky Monkey
This file contains 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: @eridbardhaj | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-chunky-monkey?solution=function%20chunk(arr%2C%20size)%20%7B%0A%20%20var%20i%3D0%3B%0A%20%20var%20edArr%20%3D%20%5B%5D%3B%0A%20%20while%20(arr.length%3E0)%20%7B%0A%20%20%20%20var%20sub%20%3D%20%5B%5D%3B%0A%20%20%20%20var%20number%20%3D%20(arr.length%20%3E%20size)%20%3F%20size%20%3A%20arr.length%3B%0A%20%20%20%20console.log(number)%3B%0A%20%20%20%20%0A%20%20%20%20for%20(var%20j%3D0%3B%20j%3Cnumber%3B%20j%2B%2B)%20%7B%0A%20%20%20%20%20%20sub.push(arr%5B0%5D)%3B%0A%20%20%20%20%20%20console.log(arr%5B0%5D)%3B%0A%20%20%20%20%20%20arr.shift()%3B%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20edArr.push(sub)%3B%0A%20%20%7D%0A%20%20%2F%2F%20Break%20it%20up.%0A%20%20return%20edArr%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%0Aconsole.log(chunk(%5B%22a%22%2C%20%22b%22%2C%20%22c%22%2C%20%22d%22%5D%2C%202))%3B%0A%0A%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function chunk(arr, size) { | |
var i=0; | |
var edArr = []; | |
while (arr.length>0) { | |
var sub = []; | |
var number = (arr.length > size) ? size : arr.length; | |
console.log(number); | |
for (var j=0; j<number; j++) { | |
sub.push(arr[0]); | |
console.log(arr[0]); | |
arr.shift(); | |
} | |
edArr.push(sub); | |
} | |
// Break it up. | |
return edArr; | |
} | |
chunk(["a", "b", "c", "d"], 2); | |
console.log(chunk(["a", "b", "c", "d"], 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment