-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
64 lines (51 loc) · 1.74 KB
/
main.js
File metadata and controls
64 lines (51 loc) · 1.74 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function divmod (a, b) {
var div = parseInt(a / b)
var rest = a % b
return [div, rest]
};
module.exports = (array, n) => {
// Verification
// array only accepts single Array
if (!Array.isArray(array)) {
throw Error("'array' needs to be a Array type")
}
// n only accepts integer numbers
if (!Number.isInteger(n) && n >= 0) {
throw Error("'n' has to be an Integer Number")
}
// Create the new splited array
var newArray = []
var markDownArray = [0]
// verify if length and 'n' size is greater then length
// which can't be
if (array.length > 0 && array.length >= n) {
// calculate div mod
var [number_each_section, extras] = divmod(array.length, n)
// generate markdown array
for (var i = 0; i < extras; i++) {
markDownArray.push(number_each_section + 1)
}
for (var i = 0; i < (n - extras); i++) {
markDownArray.push(number_each_section)
}
const accumulate = arr => arr.map((sum => value => sum += value)(0))
var points = accumulate(markDownArray)
// create subarrays
for (var i = 0; i < n; i++) {
var st = points[i]
var end = points[i + 1]
newArray.push(array.slice(st, end))
}
// return the newly created array
return newArray
} else {
// throw errors
if (array.lenght < 1) {
// error if the length is null
throw Error('The array length must be greater than zero')
} else {
// error if the size is greater them the length
throw Error('The number of requested arrays to split cannot be greater them the array itself')
}
}
}