code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

08 Chunky Monkey

/**
 * Created by Ashish Patel
 * Copyright © 2017 ashish.me
 * ashishsushilpatel@gmail.com 
 */

/**
 * Program:
 * Write a function that splits an array (first argument) into groups the length of size (second argument) and 
 * returns them as a two-dimensional array.
 */

function chunkArrayInGroups(arr, size) {
  const newarr = [];
  for(let i = 0; i < arr.length; i+=size){
    newarr.push(arr.slice(i,i+size));
  }
  return newarr;
}
  
console.log(chunkArrayInGroups(["a", "b", "c", "d", "e"], 2));

Created 2019-11-24T04:41:20+05:18 · Edit