-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
53 lines (41 loc) · 1.25 KB
/
test.js
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
const ranges = [100,10, 20, 50, 80, 5, 1];
const result = [];
const totalSum = 100;
function getSum(total, num) {
return total + Math.round(num);
}
function generator(ranges, totalSum) {
let currenSum = 0;
let totalRangeLeft = ranges.reduce(getSum);
let totalLeft = totalSum - currenSum;
function getMin(range) {
totalRangeLeft -= range;
let r = totalLeft - totalRangeLeft;
return r < 0 ? 0 : r;
}
function getMax(range) {
return totalLeft < range ? totalLeft : range;
}
function getVal(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function displayResult(val, index) {
console.log(ranges[index], val);
}
ranges.forEach((element, index) => {
const min = getMin(element);
const max = getMax(element);
const val = getVal(min, max);
currenSum += val;
totalLeft = totalSum - currenSum;
result.push(val);
displayResult(val, index);
});
console.log('Tot Range:', ranges.reduce(getSum));
console.log('Tot Generated:', result.reduce(getSum));
}
if (ranges.reduce(getSum) < totalSum) {
console.log("Total sum is too big");
return;
}
generator(ranges, totalSum);