-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerball.js
126 lines (102 loc) · 2.18 KB
/
powerball.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var powerBallLottery = {
whiteBallMax : 69,
whiteBallNum : 5,
powerBallMax : 24,
fill : function(array, max, index) {
var ball = 0;
do {
ball = this.drawBall(max);
} while (this.isAlreadyDrew(array, ball) == true);
array[index] = ball;
},
drawBall : function(max) {
var ball = (Math.random() * max) + 1;
return Math.floor(ball);
},
isAlreadyDrew : function(array, ball) {
for (var i = 0; i < array.length; i++) {
if (array[i] == ball) {
return true;
}
}
return false;
},
sortNumber : function(a, b) {
return a - b;
},
getWhiteBallsDraw : function() {
var whiteBalls = new Array(this.whiteBallNum);
for (var i = 0; i < this.whiteBallNum; i++) {
// add them to array
this.fill(whiteBalls, this.whiteBallMax, i);
}
return whiteBalls.sort(this.sortNumber);
},
getPowerBall : function() {
return this.drawBall(this.powerBallMax);
},
getNewDraw : function() {
var draw = {};
draw.whiteBalls = this.getWhiteBallsDraw();
draw.powerBall = this.getPowerBall();
return draw;
}
};
var matchPrize = {
matcheTypes : {
"00" : 0,
"10" : 0,
"20" : 0,
"01" : 4,
"11" : 4,
"21" : 7,
"30" : 7,
"31" : 100,
"40" : 100,
"41" : 50000,
"50" : 1000000,
"51" : 1500000000
},
getResult : function(officalDraw, ticket) {
var pbMatch = officalDraw.powerBall == ticket.powerBall ? 1 : 0;
var wbMatch = this.checkWhiteBallMatches(officalDraw, ticket);
return {
wbMatch : wbMatch,
pbMatch : pbMatch,
matcheTypes : this.matcheTypes,
key : "" + wbMatch + pbMatch,
completelyLost : function() {
return this.wbMatch <= 2 && pbMatch == 0;
},
getPrize : function() {
if (this.completelyLost()) {
return 0;
}
return this.matcheTypes[this.getKey()];
},
getKey : function() {
return this.key;
}
};
},
getPrize : function(result) {
},
checkWhiteBallMatches : function(officalDraw, ticket) {
var a = officalDraw.whiteBalls;
var b = ticket.whiteBalls;
var matches = 0;
var i = 0, j = 0;
while (i < a.length && j < b.length) {
if (a[i] == b[j]) {
matches++;
i++;
j++;
} else if (a[i] < b[j]) {
i++;
} else {
j++;
}
}
return matches;
}
};