freeCodeCamp Challenge Guide: Chunky Monkey

my solution is as same as yours and l like to know answer too.

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

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

My solution was similar in that I did not use a ‘push’ statement. I now have a better understanding of what ‘push’ can replace:

newArr[i] = arr.slice(i*size,(i+1)*size);

can be replaced with:

newArr.push(arr.slice(i*size,(i+1)*size));

Thanks.
I did, however, find that using 0 or a negative number for size returned the following error as mentioned above:

Error: Potential infinite loop at line (_insert number here_).
To disable loop protection, write:
// noprotect
as the first line. Beware that if you do
have an infinite loop in your code this
will crash your browser.

Tried with // noprotect and yes, it crashed the browser.

I can’t understand this code. Can any one help ?

function chunkArrayInGroups(arr, size) {
var newArr = [];
while (arr.length) {
newArr.push(arr.splice(0,size));
}
return newArr;
}

Here was my solution:

function chunkArrayInGroups(arr, size) {
  var newArr = [];
 
  if (size <= 0) {
	  window.alert("Size is 0 or less");
  } else {
	  for (var i = 0; i <= Math.ceil((arr.length / size) - 1); i++) {
      newArr[i] = arr.slice((i * size), ((i * size) + size));
      //
    }
  }
  return newArr;
}

Here was my solution - I originally tried to make too many for loops inside on another. .splice was the answer.

function chunkArrayInGroups(arr, size) {
var chunkSplice = [];
var numOfArrays = Math.ceil(arr.length/size);

for (var i=0; i<numOfArrays; i++) {
chunkSplice.push(arr.splice(0,size));
}
return chunkSplice;
}

found this to be a more functional approach to the problem
function chunkArrayInGroups(arr, size) {
return arr.reduce(function (rows, key, index) {
return (index % size == 0 ? rows.push([key])
: rows[rows.length-1].push(key)) && rows;
}, []);
}

// My small solution to this

function chunkArrayInGroups(arr, size) {
var arr2=[];
for(var i=1;i<=Math.ceil(arr.length/size);i++)
arr2[i-1]=arr.slice((i-1)size,isize);
//i * size
return arr2;
}

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

Hello Guys. it took me hours but finally i solved it by myself. here is my solution , it may seem messy but it works fine

function chunkArrayInGroups(arr, size) {
// Break it up.

var innerArr = [];

var outerArr = [];



for(var i =0;i<arr.length;i+=size){

innerArr = arr.join("").substring(i,i+size).split("");
  
    if(!isNaN(arr[0])){
      
      for(var x =0;x<innerArr.length;x++){
        
        innerArr[x] = parseInt(innerArr[x]);
      }
    }

  outerArr.push(innerArr); 

}

return outerArr;

}

I did it this way:

function chunkArrayInGroups(arr, size) {
// Break it up.

var chunks = [];

while(arr.length > 0) {
var innerChunk = [];

for(var i = 0;i < size;i++){
  var ele = arr.shift();
  
  if(ele !== undefined) {
    innerChunk.push(ele);
  }
}

chunks.push(innerChunk);

}
return chunks;
}

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

After three days, I finally broke through. Huzzah! My code seems even more basic than the basic solution, but I still feel like a champ =D

function chunkArrayInGroups(arr, size) {
  var groupedArray = [];
  var group = arr.length / size;
  if (size <= 0) {
    return arr;
  }
  for (var i = 0; i < group; i++) {
    groupedArray.push(arr.splice(0, size));
  }
  return groupedArray;
}

Hi, here’s my code solution…very long, but it works…yippeeeeee!

function chunkArrayInGroups(arr, size)
{
// Break it up.
arr2 = arr.slice(); // Make shallow copy of original array, arr
var arrlen = arr2.length; // Get array length
var quot = Math.floor(arrlen/size); // Work out the number of subgroups aka.
var rem = arrlen % size; // sub arrays by finding quotient (quot) and remainder (rem)
var groups = 0;
groups = rem > 0 ? quot + 1 : quot; // ternary operator stuff…terse!

var k = 0; // k is a counter for each element in original Array, arr
var arrComp = [];

for (var i = 0; i < groups; i++)
{
var temp = []; // create temp sub array for creating sub array groups
for(var j = 0; j < size; j++, k++)
{
if(arr[k] != undefined)
temp.push(arr[k]); // append individual array elements to end of each sub array
}
arrComp.push(temp); // push sub array to end of complete array
}
return arrComp; // Return final array, arrComp to full satisfaction!
}

// chunkArrayInGroups([“a”, “b”, “c”, “d”, 9, 8, 6, “title”, “compost”, true, false, [“canine”, “cat”, “dog”]], 5);

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

My solution.

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

This is what I came up with.

function chunkArrayInGroups(arr, size) {
 var group = [];
  
 var run = arr.length/size;
 for (var i = 0; i < run; i++){
   
   group.push(arr.splice(0, size));
 }

  return group;
}

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

I don’t really understand why isn’t anybody using shift() instead of slice()?
This is what I came up with:

function chunkArrayInGroups(arr, size) {
	var newArr = new Array(Math.ceil(arr.length / size))
	for (var i = 0; i < newArr.length; i += 1) {
		newArr[i] = [];
		for (var y = 0; y < size; y += 1) {
			if (arr.length > 0) {
				newArr[i].push(arr.shift()); 
			} else {
				return newArr;
			}
		}
	} return newArr;
}

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

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

1 Like

I didn’t want to use the SLICE function and use only PUSH Function.
It works …:slight_smile:

function chunkArrayInGroups(arr, size) {
  var arr1d =[];
  var arr2d = [];
  var times=0;
  var i=0;
  var j=0;
  var x=0;
  
  if(arr.length % size == 0)
    {
      times = (arr.length)/size;
    }
  else
    {
      times = Math.floor(arr.length/size)+1;
    }
  for(j=0;j<times;j++)
    {
        for(i=0; i<size && arr[x]!=null ; i++,x++)
          {
            arr1d.push(arr[x]);
          }
          arr2d.push(arr1d);
          arr1d=[];
              
    }
  
  
  return arr2d;
}

For me it’s easier and more clearly to use splice

function chunkArrayInGroups(arr, size) {
  // Break it up.
  var TwoDimArr = [];
  
  while (arr.length >= size) { 
    TwoDimArr.push(arr.splice(0, size)); // the use of splice is more convenient i think
  }
  
  if (arr.length !== 0) { 
    TwoDimArr.push(arr); 
  }
  
  return TwoDimArr;
}

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

ok… so I had the basic idea down for this pretty quickly but got hung up on some weird loop syntax…

this is the first one i tried, statement 3 in the loop was missing an “=” sign IE “i+=size”… this was causing it to create an infintie loop… which I dont understand why… can someone explain it to me? thanks

function chunkArrayInGroups(arr, size) {
var split_arr = [];
for (var i = 0; i < arr.length; i+size){
split_arr.push(arr.slice(i,i+size));
}

return split_arr;
}

Hi,
this is mine. Any feedback is welcome.
function chunkArrayInGroups(arr, size) {
// Break it up.
var secondArr = [],index = 0;
for(var i = 0; i < arr.length / size; i++){
secondArr.push(arr.slice(index,size+(index)));
index += size;
}
return secondArr;
}

4 Likes