problems(js): add “2677. Chunk Array”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-06-03 20:44:27 +02:00
parent d0a6ab210b
commit 28e633e89a
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,24 @@
/**
* @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;
};