Matt
Matt

Reputation: 395

JavaScript: Convert a 1D array into a 2D array by a given size

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.

Upvotes: 0

Views: 426

Answers (2)

f1ames
f1ames

Reputation: 1734

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) );

Upvotes: 1

shaochuancs
shaochuancs

Reputation: 16226

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);

Upvotes: 1

Related Questions