-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rename winning number form - separate lotto calculate(processor) logics - change winningnumbers type (from object to array) - remove logics from Lotto object
- Loading branch information
Showing
9 changed files
with
106 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { LOTTO_NUMBERS, LOTTO_WINNING_PRICE } from './constants.js'; | ||
|
||
export default class LottoProcessor { | ||
constructor(lottos, winningNumbers) { | ||
this.lottos = lottos; | ||
this.winningNumbers = winningNumbers.slice( | ||
0, | ||
LOTTO_NUMBERS.WINNING_NUMBER_COUNT - 1 | ||
); | ||
this.bonusNumber = winningNumbers[LOTTO_NUMBERS.WINNING_NUMBER_COUNT - 1]; | ||
this.rankCounts = Array(5).fill(0); | ||
this.earningRate = 0; | ||
} | ||
|
||
checkMatchingNums() { | ||
this.lottos.forEach(lotto => { | ||
const matchNumbers = [...lotto.numbers].filter(number => | ||
this.winningNumbers.includes(number) | ||
); | ||
const isMatchBonus = lotto.numbers.has(this.bonusNumber); | ||
this.updateRankCounts(matchNumbers.length, isMatchBonus); | ||
}); | ||
} | ||
|
||
updateRankCounts(matchCount, isMatchBonus) { | ||
switch (matchCount) { | ||
case 6: | ||
this.rankCounts[0]++; // 1등 | ||
break; | ||
case 5: | ||
isMatchBonus ? this.rankCounts[1]++ : this.rankCounts[2]++; // 2등 or 3등 | ||
break; | ||
case 4: | ||
this.rankCounts[3]++; // 4등 | ||
break; | ||
case 3: | ||
this.rankCounts[4]++; // 5등 | ||
break; | ||
default: | ||
this.rankCounts; | ||
} | ||
} | ||
|
||
calculateEarningRate(purchasedPrice) { | ||
const totalProfit = this.rankCounts.reduce((sum, rankCount, idx) => { | ||
return sum + rankCount * LOTTO_WINNING_PRICE[idx + 1]; | ||
}, 0); | ||
|
||
this.earningRate = (totalProfit / purchasedPrice - 1) * 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,5 @@ | ||
import { LOTTO_NUMBERS, LOTTO_WINNING_PRICE } from './constants.js'; | ||
import { LOTTO_NUMBERS } from './constants.js'; | ||
|
||
export function getRandomNumber() { | ||
return Math.floor(Math.random() * LOTTO_NUMBERS.LOTTO_MAX_NUM) + 1; | ||
} | ||
|
||
export function compareNumbers(lottos, winningNumbers) { | ||
checkMatchingNums(lottos, winningNumbers); | ||
checkBonus(lottos, winningNumbers); | ||
} | ||
|
||
function checkMatchingNums(lottos, winningNumbers) { | ||
let i = 0; | ||
while (i++ < 6) { | ||
const currNum = winningNumbers[i]; | ||
lottos.forEach(lotto => { | ||
if (lotto.numbers.has(currNum)) { | ||
lotto.addMatchNumbers(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function checkBonus(lottos, winningNumbers) { | ||
const bonusNumber = winningNumbers[7]; | ||
|
||
lottos.forEach(lotto => { | ||
if (lotto.numbers.has(bonusNumber)) { | ||
lotto.setMatchBonus(); | ||
} | ||
}); | ||
} | ||
|
||
export function countByRank(lottos, rankCounts) { | ||
lottos.forEach(lotto => { | ||
if (lotto.rank !== Infinity) { | ||
rankCounts[lotto.rank - 1] += 1; | ||
} | ||
}); | ||
} | ||
|
||
export function calculateEarningRate(rankCounts, purchasedPrice) { | ||
const totalProfit = rankCounts.reduce((sum, rankCount, idx) => { | ||
return sum + rankCount * LOTTO_WINNING_PRICE[idx + 1]; | ||
}, 0); | ||
|
||
return (totalProfit / purchasedPrice - 1) * 100; | ||
} |
Oops, something went wrong.