-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2677-chunk-array.js
More file actions
31 lines (28 loc) · 805 Bytes
/
2677-chunk-array.js
File metadata and controls
31 lines (28 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 2677. Chunk Array
* https://leetcode.com/problems/chunk-array/
* Difficulty: Easy
*
* Given an array arr and a chunk size size, return a chunked array.
*
* A chunked array contains the original elements in arr, but consists of subarrays each of
* length size. The length of the last subarray may be less than size if arr.length is not
* evenly divisible by size.
*
* You may assume the array is the output of JSON.parse. In other words, it is valid JSON.
*
* Please solve it without using lodash's _.chunk function.
*/
/**
* @param {Array} arr
* @param {number} size
* @return {Array}
*/
var chunk = function(arr, size) {
var result = [];
for (let index = 0; index < arr.length;) {
result.push(arr.slice(index, index + size));
index += size;
}
return result;
};