Just arrived at Chunky Monkey (on Basic Algorithm Scripting) and got it to work smoothly with the following code:
[code]function chunkArrayInGroups(arr, size) {
// Break it up.
var slicedArr;
var newArr=[];
for(i=0; i<arr.length; i++){
if(i%size===0){
slicedArr = arr.slice(i, size+i);
newArr.push(slicedArr);
}
}
return newArr;
}
chunkArrayInGroups([“a”, “b”, “c”, “d”], 2);
[/code]
Although, at first I was stuck because in the “slice” line I was doing:
instead of
And I don’t really understand why you have to put size+i instead of just size since, for example, if size is 2, it’s gonna slice two from where it is starting… or am I seeing this wrong? Probably not really important step but I’m just trying to understand every single of bit of these.