problems(js): add “2677. Chunk Array”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
d0a6ab210b
commit
28e633e89a
1 changed files with 24 additions and 0 deletions
24
problems/js/chunk-array.js
Normal file
24
problems/js/chunk-array.js
Normal 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;
|
||||||
|
};
|
Loading…
Reference in a new issue