Edit:found better place to post....Sorry...Chunky Monkey Solution Review...did anyone else use %?

Hey All,

I’ve done a lot of weird music algorithm coding, and from that have developed an affinity to the % operator. I used it in Chunky Monkey, and I didn’t see anyone else solving it that way. Does anyone have any advice regarding its efficiency/bugginess/style?


function chunkArrayInGroups(arr, size) {
  // Break it up.
  var chunks = [];
  
  for(var i = 0; i < arr.length; i++){
 
    if(i%size == 0) chunks.push(arr.slice(i,i+size));
    
  }
  return chunks;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
1 Like

i think that is a very elegant solution, at first I was doing it your way too using % but I could not solve it. Then eventually I used ceil …which is more convoluted than yours.
this is my solution, i love yours better …a beauty, congrats!

function chunkArrayInGroups(arr, size) {

var looptimes = (Math.ceil(arr.length/size));

var t = 0;
var newarr = [];
for (var i = 0; i < looptimes; i++) {

var temparr = arr.slice(t,t+size);
newarr.push(temparr);
t = t + size;

}
return newarr;
}

chunkArrayInGroups([“a”, “b”, “c”, “d”,‘hh’], 2);