0

Doing a freeCodeCamp challenge called "Chunky Monkey". The objective is to create a function that takes two parameters: a 1D array and a number for size.

The array is to be split into a number of groups of the length size (up to that number), thus creating a 2D array.

In my first attempt, my code was:

function chunkArrayInGroups(arr, size) {
  var set = arr.length / size;
  var count = 0;
  set = Math.ceil(set); //ensure that an integer is obtained
  var array = [];
  for (var i = 0; i < set; i++) {
    array[i] = []; //ensure each element i is an array
    for (var j = 0; j < size; j++) {
      array[i][j] = arr[count]; //obtain values from passed array, arr
      count++;
    }
  }
  return array;
}

var result = chunkArrayInGroups(["a", "b", "c", "d"], 2);
console.log(result);

I have traced this code a few times, but I cannot tell why it is wrong. Can someone please spot the error in it?

I eventually tackled the problem using a slightly different method, and it worked, but I am very interested in why the code above did not work.

6
  • 2
    Hey but it is working Commented Jun 27, 2017 at 6:15
  • 1
    code is working Commented Jun 27, 2017 at 6:15
  • i think the problem here is it always creates an array with the specified length. So when you call chunkArrayInGroups(["a", "b", "c", "d"], 2), you get [["a", "b", "c" ],["d", undefined,undefined]] Commented Jun 27, 2017 at 6:19
  • If chunkArrayInGroups(["a", "b", "c", "d", "e"], 2);, what is the expected result? [["a", "b"],["c", "d"],["e", undefined]] or [["a", "b"],["c", "d"],["e"]]? Commented Jun 27, 2017 at 6:19
  • It seems to only work some of the times, for example, it failed this test among others: chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) should return [[0, 1, 2, 3], [4, 5, 6, 7], [8]]. Commented Jun 27, 2017 at 6:20

2 Answers 2

1

You need to break the loop once count reach the max limit:

function chunkArrayInGroups(arr, size) {
  var set = arr.length/size; 
  var count = 0; 
  set = Math.ceil(set); //ensure that an integer is obtained
  var array = []; 
  out:
  for(var i = 0; i<set; i++){
    array[i] = []; //ensure each element i is an array
    for (var j=0; j<size; j++){
      if (count === arr.length) {
        break out;
      }
      array[i][j] = arr[count]; //obtain values from passed array, arr
      count++; 
    }
  }      
  return array;
}

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

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

The code is working (with some exceptions, see result of console.log( chunkArrayInGroups(["a", "b", "c", "d"], 5) ); and @shaochuancs answer), but you may try a simpler approach utilizing native methods, like (see chunk function):

function chunkArrayInGroups(arr, size) {
  var set = arr.length/size; 
  var count = 0; 
  set = Math.ceil(set); //ensure that an integer is obtained
  var array = []; 
  for(var i = 0; i<set; i++){
    array[i] = []; //ensure each element i is an array
    for (var j=0; j<size; j++){
      array[i][j] = arr[count]; //obtain values from passed array, arr
      count++; 
    }
  }      
  return array;
}

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

console.log( chunkArrayInGroups(["a", "b", "c", "d"], 2) );
console.log( chunkArrayInGroups(["a", "b", "c", "d"], 5) );
console.log( chunkArrayInGroups([], 2) );

console.log( chunk(["a", "b", "c", "d"], 2) );
console.log( chunk(["a", "b", "c", "d"], 5) );
console.log( chunk([], 2) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.