-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloterry.js
More file actions
172 lines (146 loc) · 4.46 KB
/
loterry.js
File metadata and controls
172 lines (146 loc) · 4.46 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
KEY_SIZE = 5
var num_key = new Array();
function init_lottery(){
for (i = 0; i < KEY_SIZE; i++) {
num_key[i] = Math.floor(Math.random() * 48) + 1;
}
num_key[KEY_SIZE] = Math.floor(Math.random() * 18) + 1 ;
display_array(num_key);
selection_sort(num_key);
display_sorted_array(num_key);
}
function display_array(array) {
for (i = 0; i < 6; i++){
document.getElementById('ai' + (i + 1)).innerHTML = array[i];
}
}
function display_sorted_array(array) {
for (i = 0; i < 6; i++){
document.getElementById('sai' + (i + 1)).innerHTML = array[i];
}
}
/*I could have used the array method ".sort" here, but I wanted to brush
up my selection sort.*/
function selection_sort(array) {
var min_ind;
for (i = 0; i < KEY_SIZE; i++) {
min_ind = i;
for (j = i + 1; j < KEY_SIZE; j++) {
if (array[j] < array[min_ind]) {
min_ind = j;
}
}
if (i !== min_ind) {
array = swap(array, i, min_ind);
}
}
}
function swap(array, ind1, ind2) {
var temp;
temp = array[ind1];
array[ind1] = array[ind2];
array[ind2] = temp;
return array;
}
function takeInput() {
var nums_input, lucky_input, user_guess;
nums_input = document.getElementById('norm_nums').value;
lucky_input = document.getElementById('lucky_num').value;
nums_input = nums_input +' ' + lucky_input;
user_guess = nums_input.split(" ");
for (i = 0; i <= KEY_SIZE; i++) {
user_guess[i] = parseInt(user_guess[i], 10);
}
payout_calc(user_guess);
}
function payout_calc(user_guess) {
var count = 0, temp_user, temp_key, len, lucky = false;
temp_user = user_guess;
temp_key = num_key;
if (temp_user[KEY_SIZE] === num_key[KEY_SIZE]) {
lucky = true;
}
temp_user.pop();
temp_key.pop();
for (i = 0; i < temp_user.length; i++) {
for (j = 0; j < temp_key.length; j++) {
if (temp_user.includes(temp_key[j])) {
count++;
temp_key.splice(j, 1);
console.log(temp_user);
console.log(temp_key);
}
}
}
//document.write(count);
winnings(count, lucky);
}
function winnings(count, lucky) {
switch (count) {
case 5:
if (lucky === true) {
prize = "$7,000 a WEEK for LIFE";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers and the lucky ball! That means you've won: " + prize + ".";
}
else {
prize = "$25,000 a YEAR for LIFE";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers but not the lucky ball! That means you've won: " + prize + ".";
}
break;
case 4:
if (lucky === true) {
prize = "$5,000";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers and the lucky ball! That means you've won: " + prize + ".";
}
else {
prize = "$200";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers but not the lucky ball! That means you've won: " + prize + ".";
}
break;
case 3:
if (lucky === true) {
prize = "$150";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers and the lucky ball! That means you've won: " + prize + ".";
}
else {
prize = "$20";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ "numbers but not the lucky ball! That means you've won: " + prize + ".";
}
break;
case 2:
if (lucky === true) {
prize = "$25";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers and the lucky ball! That means you've won: " + prize + ".";
}
else {
prize = "$3";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers but not the lucky ball! That means you've won: " + prize + ".";
}
break;
case 1:
if (lucky === true) {
prize = "$6";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " number and the lucky ball! That means you've won: " + prize + ".";
break;
}
case 0:
if (lucky === true) {
prize = "$4";
document.getElementById('results').innerHTML = "Congrats, you matched " + count
+ " numbers and the lucky ball! That means you've won: " + prize + ".";
break;
}
default:
document.getElementById('results').innerHTML = "No winnings this time! Try again soon!";
}
}
init_lottery();