LeetCode/problems/js/chunk-array.js
Matej Focko 28e633e89a
problems(js): add “2677. Chunk Array”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-03 20:44:27 +02:00

24 lines
450 B
JavaScript

/**
* @param {Array} arr
* @param {number} size
* @return {Array[]}
*/
var chunk = function(arr, size) {
let chunked = [];
let current = new Array();
for (let i = 0; i < arr.length; ++i) {
current.push(arr[i]);
if ((i + 1) % size == 0) {
chunked.push(current);
current = new Array();
}
}
if (current.length > 0) {
chunked.push(current);
}
return chunked;
};