:hammer: Uplift the function to calculate interest greater than $2000.00 · FagnerMartinsBrack/jack-the-moneylender@408a2cd
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 @@
77assert.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+1016const interestToPayFor = (loanAmount) => {
1117const END_OF_FIRST_RANGE = Money('$2000.00');
1218const END_OF_SECOND_RANGE = Money('$5000.00');
@@ -18,20 +24,14 @@
1824const CENTS_FOR_FOURTH_RANGE = Money('$0.21');
19252026if (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-};
2627const interestAmount = calculateInterestForGreaterThan2000(loanAmount, END_OF_FIRST_RANGE, CENTS_FOR_SECOND_RANGE);
2728return interestAmount;
2829}
29303031if (loanAmount.greaterThan(END_OF_SECOND_RANGE) && loanAmount.lessThan(Money('$10001.00'))) {
3132let 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);
3535interestAmount = interestAmount.plus(interestAmountForSecondRange);
36363737const dollarsAboveThresholdForThirdRange = loanAmount.minus(END_OF_SECOND_RANGE);
@@ -44,8 +44,7 @@
4444if (loanAmount.greaterThan(END_OF_THIRD_RANGE)) {
4545let 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);
4948interestAmount = interestAmount.plus(interestAmountForSecondRange);
50495150const dollarsAboveThresholdForThirdRange = loanAmount.minus(END_OF_SECOND_RANGE);