code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

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:53:20+05:30 · Edit