-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_chunkyMonkey.js
More file actions
29 lines (26 loc) · 779 Bytes
/
16_chunkyMonkey.js
File metadata and controls
29 lines (26 loc) · 779 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
/*
Chunky Monkey
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) {
let arr2D = [];
for (let index = 0; index < arr.length; index += size) {
arr2D.push(arr.slice(index, index + size));
// console.log(index, arr2D);
}
return arr2D;
}
const testData = [
[[0, 1, 2, 3, 4, 5, 6, 7, 8], 2],
[[0, 1, 2, 3, 4, 5, 6, 7, 8], 4],
[[0, 1, 2, 3, 4, 5, 6], 3],
[[0, 1, 2, 3, 4, 5], 4],
[[0, 1, 2, 3, 4, 5], 2],
[[0, 1, 2, 3, 4, 5], 3],
[["a", "b", "c", "d"], 2],
];
for (let i = 0; i < testData.length; i++) {
console.log(chunkArrayInGroups(testData[i][0], testData[i][1]));
}