freeCodeCamp Challenge Guide: Chunky Monkey

function chunkArrayInGroups(arr, size) {

  if(size <= 0) return arr;

  var newArr = [];
  

    //Loop through the array and slice it from i to i + size,
        //Increment i in the slice argument.
      for(var i=0; i < arr.length;) {
          newArr.push(arr.slice(i, i+=size));
      }
    
      return newArr;
}

Is it ok to increment in an argument like this? Or is it bad practice? How can I test this for speed? Thanks in advance

//George

1 Like

I love seeing the different solutions here! This is what I’ve come up with, took me forever! :weary:

function chunkArrayInGroups(arr, size) {
  // Break it up. 
  var newArr = []; 
  for (var i = 0; i < (arr.length / size); i++) {  
   if (size === 0){ break; } // Tried size <=0 to prevent infinite loop...no luck 
   newArr.push(arr.slice((i * size), (i * size) + size));
  }   
  return newArr;  
} 

Is this solution efficient? Any suggestions on how could I make it faster? Thanks guys! :ghost::zap:

1 Like

This is my solution :slight_smile:
function chunkArrayInGroups(arr, size) {
var newArr = [];
for(var i=0;i<arr.length ;i += size){
var arr1 = arr.slice(i,i+size);
newArr.push(arr1);

}

return newArr;
}

1 Like

Here is a solution without .slice()

function chunkArrayInGroups(arr, size) {
  var result = [];
  var temp = [];
  for (var i = 0; i < arr.length; i += size) {
    temp = [];
    for (var j = i; j < Math.min(i + size, arr.length); j++) {
      temp.push(arr[j]);
    }
    result.push(temp);
  }
  return result;
}

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

1 Like
function chunkArrayInGroups(arr, size) {
  var finalArray = []; 
  for (var i = 0; i < (arr.length / size); i++){ 
      finalArray.push(arr.slice((i * size), ((i*size) + size)));
      }
  return finalArray;
}

((i*size) + size) was the only end parameter it ocurred to me, but I think it’s ugly :disappointed_relieved:

I find it frustrating that I was unable to solve this challenge. I finally gave up and looked online for a solution. It turns out I was on the right track, but my math was a little off. For some reason I really struggle with arrays, but I find most of the other challenges to be easy.

4 Likes

Thank you for sharing your solution. I came up with pretty much the same one except I didn’t incorporate the stop variable. I could figure why my code wouldn’t pass. I guess I didn’t understand how the slice method worked. Thanks again.

Hi,

Here is my solution:

function chunkArrayInGroups(arr, size) {

  var chunk   = [];
  var chunks  = [];
  var index   = 0;
  
  for (var i=0; i<arr.length; i++){
    
    chunk.push(arr.slice(index, index+size));
    index+=size;
  }
  for (var y=0; y<chunk.length; y++){
    //clean our array from empty arrays
    if(chunk[y].length !== 0){
      chunks.push(chunk[y]);
    }

  }
  
  return chunks;
}

//test
chunkArrayInGroups(["a", "b", "c", "d"], 2);

Can someone please explain in regards to the basic code solution, why using if (a % size !== size - 1) is a good solution? Just a little confused as I’ve never seen a modulus used in an if statement.

Thanks

3 Likes

I spent 2 hours on this, but came out with an intermediate solution. Im proud of my self :smiley:
function chunkArrayInGroups(arr, size) {
// Break it up.
arr1= [];
for (i=0; i<arr.length/size; i++){
arr1.push(arr.slice(sizei, size(i+1)));
} return arr1;
//console.log(arr.slice(sizei, size(i+1)));
//console.log(arr.shift(1));
}
chunkArrayInGroups([ā€œaā€, ā€œbā€, ā€œcā€, ā€œdā€], 2);

I came up with this. I would greatly appreciate feedback.

function chunkArrayInGroups(arr, size) {
  arrayContainer = [];
  for (i = 0; i < arr.length; i += size){
      if (i === 0 || i % size === 0) {
        arrayContainer.push(arr.slice(0 + i, i + size));
      }
  }
  return (arrayContainer);
}
1 Like

Hi, this is my solution. Can anyone advice me on this. thank you in advance :slight_smile:

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

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2);

My solution. Any feedback on how good or bad it is are much appreciated.

function chunkArrayInGroups(arr, size) {
  
  var topArray = [];
  i = 0;
  var begin = 0;
  var end = size;
  
  while (i < arr.length/size) {
    topArray.push(arr.slice(begin,end));
    i++;
    begin += size;
    end += size;
  }
  return topArray;
}
1 Like

Hey, can I ask what’s the benefit of calling the function inside of its chunk and passing the same parameters as an arguments ?

function chunkArrayInGroups(arr, size) {
var arr1 = ;
var cal = Math.ceil(arr.length / size);
// calculate the No of subarrays needed using this equation.
for(var i = 0; i < cal; i++){
arr1.push(arr.splice(0, size));
//using splice as it change the original array so every time for loop will
//start from the last position it ends at the time before.
}
return arr1;
}

1 Like

friends… can anyone help me on this…
where have i gone wrong…

function chunkArrayInGroups(arr, size) {
// Break it up.
var a1=[][size];
for(var i=0;i<arr.size;i++)
{
for(var j=0;j<size;j++)
{
a1[i][j]=arr[j];
}
}
//arr=a1;
return a1;

}

Just wanted to reiterate what @luishendrix92 had mentioned at the beginning of this thread. It seems that many of the solutions posted, (including the ones in the Wiki) will produce an infinite loop when the size passed is 0 or less!

I believe it would be prudent for everyone to check their code against this scenario, because I don’t think anyone wants a piece of code causing this issue.

Perhaps this scenario should be addressed in the Wiki and the exercise itself?

Thanks @luishendrix92! I certainly wouldn’t have caught this problem myself!

function chunkArrayInGroups(arr, size) {
// Break it up.
var loop = Math.ceil(arr.length/size);
var new_array = [];
var start = 0;
var end = size;

for(var i=0; i<loop; i++){
new_array.push(arr.slice(start, end));
start = start + size;
end = end + size;
}

return new_array;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2);

function chunkArrayInGroups(arr, size) {
  var x = 0;
  var arr2=[];
  // Break it up.
  while(size*x<arr.length){
    arr2.push(arr.slice((size*x),(size*x+size)));
    x++;
  }
  
  return arr2;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2);

I kept it simple and straight forward

function chunkArrayInGroups(arr, size) {
  
  //Index calculates numbers of subarrays in newArr
  let newArr = [];
  let index = arr.length/size;
  
  //Slice chunks old array into new
  for (let i =0; i<index; i++){
    newArr[i] = arr.slice(i*size,(i+1)*size);
  }
  
  return newArr;
}

chunkArrayInGroups(["a", "b", "c", "d", "e", "f"], 2);

brief explanation:

If you know the array is 6 units long, stored in 2 array chunks, that means you have 3 subarrays.

This means following:

//input array [a,b,c,d,e,f]
arr.slice(0,2)  // [a,b]
arr.slice(2,4)  // [c,d]
arr.slice(4,6)  // [e,f]

will produce the results you want. It runs three times total. The same as the number of subarrays

so you can use a for loop to produce the arr.slice three times