Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as JavaScript by Mojilla ( 6 years ago )
devideBy([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]


function devideBy(array, size) {
  const chunked_arr = [];
  let index = 0;

  while (index < array.length) {
    chunked_arr.push(array.slice(index, size + index));
    index += size;
  }
  return chunked_arr;
}

// recursive way
function devideBy(array, size) {
  if (!array) return [];

  const firstChunk = array.slice(0, size); // create the first chunk of the given array
  if (!firstChunk.length) {
    return array; // this is the base case to terminal the recursive
  }

  return [firstChunk].concat(chunk(array.slice(size, array.length), size)); 
}

 

Revise this Paste

Children: 102757
Your Name: Code Language: