-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlotto.js
More file actions
175 lines (147 loc) · 4.6 KB
/
lotto.js
File metadata and controls
175 lines (147 loc) · 4.6 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
173
174
175
'use strict';
const lottoInfo = {
'minNum': 1,
'maxNum': 45,
'length': 6,
'price': 1000,
'prizeList': [{
'matchingCount': 3,
'reward': 5000
},
{
'matchingCount': 4,
'reward': 50000
},
{
'matchingCount': 5,
'reward': 1500000
},
{
'matchingCount': 6,
'reward': 2000000000
}]
};
// Buy Lottos
function buyLottos(money, lotto = lottoInfo) {
try {
validateMoney(money, lotto.price);
} catch (err) {
return console.error(err);
}
// Get number of lottos for money
const numberOfLottos = getNumberOfLottos(money, lotto.price);
// Create list of lottos
const lottoSetList = [];
while (lottoSetList.length < numberOfLottos) {
lottoSetList.push(getLottoSet(lotto));
}
return lottoSetList;
}
function validateMoney(money, minAmount) {
if (!Number.isInteger(money)) {
throw `>> [!] 잘못된 입력입니다.`;
}
if (money < minAmount) {
throw `>> [!] ${minAmount}원 이상의 금액을 입력해주세요.`;
}
}
function getNumberOfLottos(money, lottoPrice) {
return Math.floor(money / lottoPrice);
}
function getLottoSet(lotto) {
const lottoSet = [];
while (lottoSet.length < lotto.length) {
let lottoNumber = getRandomNumber(lotto.minNum, lotto.maxNum);
if (!lottoSet.includes(lottoNumber)) lottoSet.push(lottoNumber);
}
lottoSet.sort((num1, num2) => num1 - num2);
return lottoSet;
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * max + min);
}
// Set lucky number
function setLuckyNumber(luckyNum, lotto = lottoInfo, lottoSetList = lottoList) {
try {
validateLuckyNum(luckyNum, lotto);
} catch (err) {
return console.error(err);
}
// Get result match with lucky number for all lottos
const matchingResultList = lottoSetList.map(lottoSet =>
getMatchingResult(luckyNum, lottoSet)
);
// Get winning statistic
const winningStat = getWinningStatistic(matchingResultList, lotto.prizeList);
showWinningStatistic(winningStat, lotto.prizeList);
// Rate of return
const rateOfReturn = getRateOfReturn(winningStat, lotto, lottoSetList);
console.log(`>> 나의 수익률은 ${rateOfReturn}% 입니다.`);
}
function validateLuckyNum(luckyNum, lotto) {
if (!Array.isArray(luckyNum)) {
throw `>> [!] 배열 형태의 당첨번호를 입력해주세요.`;
}
if (luckyNum.filter(val => val !== undefined).length !== lotto.length) {
throw `>> [!] ${lotto.length}자리의 당첨번호를 입력해주세요.`;
}
if (!luckyNum.every(num => Number.isInteger(num))) {
throw `>> [!] 숫자로 된 당첨번호를 입력해주세요.`;
}
if (!luckyNum.every(num => num >= lotto.minNum && num <= lotto.maxNum)) {
throw `>> [!] ${lotto.minNum}과 ${lotto.maxNum}사이의 당첨번호를 입력해주세요.`;
}
}
function getMatchingResult(luckyNum, lottoSet) {
const matchingNums = lottoSet.filter(num => luckyNum.includes(num));
const matchingCount = matchingNums.length;
return {
lottoSet,
matchingNums,
matchingCount
};
}
function getWinningStatistic(matchingResultList, prizeList) {
// Make winning statistic object and initializing
const winningStatistic = prizeList.reduce((winningStat, prizeInfo) => {
winningStat[prizeInfo.matchingCount] = 0;
return winningStat;
}, {});
// Count winning result
matchingResultList.forEach(matchingResult => {
if (matchingResult.matchingCount in winningStatistic) {
winningStatistic[matchingResult.matchingCount]++;
}
});
return winningStatistic;
}
function showWinningStatistic(winningStat, prizeList) {
const resultMsg = prizeList.reduce((msg, prizeInfo) => {
return msg += `${prizeInfo.matchingCount}개 일치 (${prizeInfo.reward}원) - `
+ `${winningStat[prizeInfo.matchingCount]}개\n`;
}, '');
console.log(`>> 당첨 통계`);
console.log(`----------`);
console.log(resultMsg);
}
function getRateOfReturn(winningStat, lotto, lottoSetList) {
const moneySpent = lotto.price * lottoSetList.length;
const totalProfit = lotto.prizeList.reduce((profit, prizeType) => {
if (winningStat[prizeType.matchingCount]) {
profit += winningStat[prizeType.matchingCount] * prizeType.reward;
}
return profit;
}, 0);
const rateOfReturn = (totalProfit - moneySpent) / moneySpent * 100;
return rateOfReturn < 0 ? 0 : Math.floor(rateOfReturn);
}
function showLottoSetList(lottoSetList) {
console.log(`>> 로또 ${lottoSetList.length}개를 발행했습니다.`);
lottoSetList.forEach(lottoSet => console.log(lottoSet));
}
// Run
const lottoList = buyLottos(5000);
if (Array.isArray(lottoList)) {
showLottoSetList(lottoList);
setLuckyNumber([1, 2, 3, 4, 5, 6]);
}