Skip to content

Commit

Permalink
refactor: refactoring
Browse files Browse the repository at this point in the history
- change earning rate
  • Loading branch information
goni-ssi committed Feb 26, 2021
1 parent fec9c70 commit a87fe1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/js/model/LottoMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class LottoMachine {
let matchBonus = false;

lotto.numbers.forEach(number => {
if (mainNumbers.includes(number)) {
if (mainNumbers.find(mainNumber => mainNumber === number)) {
matchCount++;
}
if (bonusNumber === number) {
Expand All @@ -96,13 +96,15 @@ export class LottoMachine {
}

calculateEarning(rankCounts) {
return rankCounts.reduce((earning, count, rank) => {
if (rank === 0) {
return earning;
}

return (earning += PRIZE_MONEY[rank] * count);
}, 0);
return (
rankCounts.reduce((earning, count, rank) => {
if (rank === 0) {
return earning;
}

return (earning += PRIZE_MONEY[rank] * count);
}, 0) - this.#insertedMoney
); // 수익 = (당첨금 - 투입금)
}

reset() {
Expand Down
8 changes: 4 additions & 4 deletions src/js/util/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
MSG_OVERLAPPED_LOTTO_NUMBERS,
} from '../constants/alertMessage.js';
import {
BONUS_NUMBER_COUNT,
LOTTO_NUMBER_COUNT,
WINNING_NUMBER_COUNT,
MAX_LOTTO_NUMBER,
MIN_LOTTO_NUMBER,
MSG_INVALID_PURCHASE_AMOUNT,
Expand All @@ -25,15 +24,16 @@ export const validator = {
return '';
},
lottoNumbers: numbers => {
if (numbers.length < LOTTO_NUMBER_COUNT + BONUS_NUMBER_COUNT) {
if (numbers.length < WINNING_NUMBER_COUNT) {
return MSG_BLANK_INPUT;
}
if (!isRangeOf(MIN_LOTTO_NUMBER, MAX_LOTTO_NUMBER, numbers)) {
return MSG_OUT_RANGED_LOTTO_NUMBERS;
}
if (isOverlapped(numbers)) {
console.log(numbers);
return MSG_OVERLAPPED_LOTTO_NUMBERS;
}

return '';
},
};
11 changes: 7 additions & 4 deletions src/js/view/LottoView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { $ } from '../util/index.js';

export class LottoView {
renderLottoSection(lottos) {
this.$lottoSection = $('#lotto-section');
this.$lottoResultForm = $('#lotto-result-form');

$('#lotto-count').innerText(`${lottos.length}`);
$('#lotto-container').innerHTML(lottoTemplate(lottos));
$('#lotto-section').show();
$('#lotto-result-form').show();
this.$lottoSection.show();
this.$lottoResultForm.show();

function lottoTemplate(lottos) {
return lottos.reduce((html, lotto, idx) => {
Expand All @@ -31,7 +34,7 @@ export class LottoView {
}

reset() {
$('#lotto-section').hide();
$('#lotto-result-form').hide();
this.$lottoSection.hide();
this.$lottoResultForm.hide();
}
}

0 comments on commit a87fe1e

Please sign in to comment.