Readplace

:hammer: Uplift the function to calculate interest greater than $2000.00 · FagnerMartinsBrack/jack-the-moneylender@408a2cd

GitHub 1 min read
View original
  • current
Summary (TL;DR)
This diff shows a code refactoring that extracts a duplicated calculation into a reusable function named calculateInterestForGreaterThan2000. The function computes interest for loan amounts above $2000 by subtracting the threshold from the loan and multiplying by a cents rate. It is then called in three conditional branches handling different loan tiers, replacing inline calculations. The change improves code maintainability and reduces duplication.

@@ -7,6 +7,12 @@

77

assert.strictEqual(actual, expected);

88

}

9910+

const calculateInterestForGreaterThan2000 = (loanAmount, endOfFirstRange, centsForSecondRange) => {

11+

const dollarsAboveThresholdForGreaterThan2000 = loanAmount.minus(endOfFirstRange);

12+

const interestForGreaterThan2000 = centsForSecondRange.multipliedBy(dollarsAboveThresholdForGreaterThan2000);

13+

return interestForGreaterThan2000;

14+

};

15+1016

const interestToPayFor = (loanAmount) => {

1117

const END_OF_FIRST_RANGE = Money('$2000.00');

1218

const END_OF_SECOND_RANGE = Money('$5000.00');

@@ -18,20 +24,14 @@

1824

const CENTS_FOR_FOURTH_RANGE = Money('$0.21');

19252026

if (loanAmount.greaterThan(END_OF_FIRST_RANGE) && loanAmount.lessThan(Money('$5001.00'))) {

21-

const calculateInterestForGreaterThan2000 = (loanAmount, endOfFirstRange, centsForSecondRange) => {

22-

const dollarsAboveThresholdForGreaterThan2000 = loanAmount.minus(endOfFirstRange);

23-

const interestForGreaterThan2000 = centsForSecondRange.multipliedBy(dollarsAboveThresholdForGreaterThan2000);

24-

return interestForGreaterThan2000;

25-

};

2627

const interestAmount = calculateInterestForGreaterThan2000(loanAmount, END_OF_FIRST_RANGE, CENTS_FOR_SECOND_RANGE);

2728

return interestAmount;

2829

}

29303031

if (loanAmount.greaterThan(END_OF_SECOND_RANGE) && loanAmount.lessThan(Money('$10001.00'))) {

3132

let interestAmount = Money('$0.00');

323333-

const dollarsAboveThresholdForSecondRange = loanAmount.minus(END_OF_FIRST_RANGE);

34-

const interestAmountForSecondRange = CENTS_FOR_SECOND_RANGE.multipliedBy(dollarsAboveThresholdForSecondRange);

34+

const interestAmountForSecondRange = calculateInterestForGreaterThan2000(loanAmount, END_OF_FIRST_RANGE, CENTS_FOR_SECOND_RANGE);

3535

interestAmount = interestAmount.plus(interestAmountForSecondRange);

36363737

const dollarsAboveThresholdForThirdRange = loanAmount.minus(END_OF_SECOND_RANGE);

@@ -44,8 +44,7 @@

4444

if (loanAmount.greaterThan(END_OF_THIRD_RANGE)) {

4545

let interestAmount = Money('$0.00');

464647-

const dollarsAboveThresholdForSecondRange = loanAmount.minus(END_OF_FIRST_RANGE);

48-

const interestAmountForSecondRange = CENTS_FOR_SECOND_RANGE.multipliedBy(dollarsAboveThresholdForSecondRange);

47+

const interestAmountForSecondRange = calculateInterestForGreaterThan2000(loanAmount, END_OF_FIRST_RANGE, CENTS_FOR_SECOND_RANGE);

4948

interestAmount = interestAmount.plus(interestAmountForSecondRange);

50495150

const dollarsAboveThresholdForThirdRange = loanAmount.minus(END_OF_SECOND_RANGE);