-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
23 lines (19 loc) · 701 Bytes
/
index.js
File metadata and controls
23 lines (19 loc) · 701 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
*
* @param {array} arrayElements - The original array of numbers
* @param {number} groupedInto - The number that the array should be grouped by
* @returns {array} A new array of grouped elements
*/
const groupArrayElements = (arrayElements, groupedInto) => {
let groupedArrayElements = [];
const arrayElementsLength = arrayElements.length;
const groupSize = Math.ceil(arrayElementsLength / groupedInto);
for (let i = 0; i < groupedInto; i += 1) {
const start = i * groupSize;
const end = (i + 1) * groupSize;
const group = arrayElements.slice(start, end);
groupedArrayElements.push(group);
}
return groupedArrayElements;
}
module.exports = groupArrayElements;