-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
59 lines (58 loc) · 1.76 KB
/
utils.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
54
55
56
57
58
59
let suits = ["hearts", "diamonds", "clubs", "spades"];
module.exports = {
otherColor: function(suit) {
return {
spades: "clubs",
clubs: "spades",
diamonds: "hearts",
hearts: "diamonds"
}[suit];
},
randomSuit: function() {
return suits[Math.floor(Math.random() * suits.length)];
},
/**
* Shuffles array in place.
* @param {Array} a items An array containing the items.
*/
shuffle: function(a) {
let j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
},
deepCopy: function(x) {
return JSON.parse(JSON.stringify(x));
},
splitWinnings: function(remainder, numWinners) {
let remainingRewards = [];
if (numWinners == 1 || numWinners == 2 || numWinners == 4) {
console.log("1 2 4");
for (let i = 0; i < numWinners; i++) {
remainingRewards.push(remainder / numWinners);
}
} else if (numWinners == 3) {
console.log("3 winners");
if (remainder % 3 == 0) {
for (let i = 0; i < numWinners; i++) {
remainingRewards.push(remainder / numWinners);
}
} else if (remainder % 3 == 1) {
console.log("remainder is 1 mod 3");
console.log("math.floor: " + Math.floor(remainder / 3));
remainingRewards.push(Math.floor(remainder / 3));
remainingRewards.push(Math.floor(remainder / 3));
remainingRewards.push(Math.floor(remainder / 3) + 1);
} else if (remainder % 3 == 2) {
remainingRewards.push(Math.floor(remainder / 3));
remainingRewards.push(Math.floor(remainder / 3) + 1);
remainingRewards.push(Math.floor(remainder / 3) + 1);
}
}
return remainingRewards;
}
};