|
1 |
| -const logger = (() => { |
2 |
| - |
3 |
| - let logBuffer = []; |
4 |
| - |
5 |
| - return { |
6 |
| - |
7 |
| - append: message => { |
8 |
| - |
9 |
| - logBuffer = logBuffer.concat (message); |
10 |
| - |
11 |
| - }, |
12 |
| - |
13 |
| - flush: () => { |
14 |
| - |
15 |
| - console.log (logBuffer.join ('\r\n')); |
16 |
| - logBuffer = []; |
17 |
| - |
18 |
| - } |
19 |
| - }; |
20 |
| -})(); |
21 |
| - |
22 |
| -const numToCurrency = symbol => amount => ( |
23 |
| - |
24 |
| - symbol + amount.toFixed (2) |
25 |
| - |
26 |
| -); |
27 |
| - |
28 |
| - |
29 |
| -const monthlyFigures = ({currentBalance, monthlyRate, minimumRate}) => { |
30 |
| - |
31 |
| - const monthlyPayment = currentBalance * minimumRate; |
32 |
| - const interestPayment = currentBalance * monthlyRate; |
33 |
| - const principalPayment = monthlyPayment - interestPayment; |
34 |
| - |
35 |
| - return { |
36 |
| - |
37 |
| - newBalance: currentBalance - principalPayment, |
38 |
| - monthlyPayment, |
39 |
| - principalPayment |
40 |
| - |
41 |
| - }; |
42 |
| - |
43 |
| -}; |
44 |
| - |
45 |
| - |
46 |
| -const runningBalance = maxMonths => |
47 |
| - ({annualRate, minimumRate, startingBalance}) => { |
48 |
| - |
49 |
| - const runningBalanceRecur = |
50 |
| - ({currentBalance, totalPaid, monthIndex}) => { |
51 |
| - |
52 |
| - if (monthIndex === maxMonths) |
53 |
| - return {currentBalance, totalPaid}; |
54 |
| - |
55 |
| - let {newBalance, monthlyPayment, principalPayment} = |
56 |
| - monthlyFigures ({ |
57 |
| - |
58 |
| - currentBalance, |
59 |
| - monthlyRate: annualRate / 12, |
60 |
| - minimumRate |
61 |
| - |
62 |
| - }); |
63 |
| - |
64 |
| - const numToDollars = numToCurrency ('$'); |
65 |
| - logger.append ('Month: ' + (monthIndex + 1)); |
66 |
| - logger.append ('Minimum monthly payment: ' + |
67 |
| - numToDollars (monthlyPayment)); |
68 |
| - logger.append ('Principle paid: ' + |
69 |
| - numToDollars (principalPayment)); |
70 |
| - logger.append ('Remaining balance: ' + numToDollars (newBalance)); |
71 |
| - |
72 |
| - return runningBalanceRecur ({ |
73 |
| - |
74 |
| - currentBalance: newBalance, |
75 |
| - totalPaid: totalPaid + monthlyPayment, |
76 |
| - monthIndex: monthIndex + 1 |
77 |
| - |
78 |
| - }); |
79 |
| - |
80 |
| - }; |
81 |
| - |
82 |
| - return runningBalanceRecur ({ |
83 |
| - |
84 |
| - currentBalance: startingBalance, |
85 |
| - totalPaid: 0, |
86 |
| - monthIndex: 0 |
87 |
| - |
88 |
| - }); |
89 |
| -}; |
90 |
| - |
91 |
| - |
92 |
| -const main = annualRate => minimumRate => startingBalance => { |
93 |
| - |
94 |
| - let {currentBalance, totalPaid} = runningBalance (12)({ |
95 |
| - annualRate, minimumRate, startingBalance}); |
96 |
| - |
97 |
| - const numToDollars = numToCurrency ('$'); |
98 |
| - logger.append ('RESULT'); |
99 |
| - logger.append ('Total amount paid: ' + numToDollars (totalPaid)); |
100 |
| - logger.append ('Remaining balance: ' + numToDollars (currentBalance)); |
101 |
| - logger.flush (); |
102 |
| - return true; |
103 |
| - |
104 |
| -}; |
105 |
| - |
106 |
| - |
107 |
| -module.exports = main; |
108 |
| -
|
| 1 | +const logger = (() => { |
| 2 | + |
| 3 | + let logBuffer = []; |
| 4 | + |
| 5 | + return { |
| 6 | + |
| 7 | + append: message => { |
| 8 | + |
| 9 | + logBuffer = logBuffer.concat (message); |
| 10 | + |
| 11 | + }, |
| 12 | + |
| 13 | + flush: () => { |
| 14 | + |
| 15 | + console.log (logBuffer.join ('\r\n')); |
| 16 | + logBuffer = []; |
| 17 | + |
| 18 | + } |
| 19 | + }; |
| 20 | +})(); |
| 21 | + |
| 22 | +const numToCurrency = symbol => amount => ( |
| 23 | + |
| 24 | + symbol + amount.toFixed (2) |
| 25 | + |
| 26 | +); |
| 27 | + |
| 28 | + |
| 29 | +const monthlyFigures = ({currentBalance, monthlyRate, minimumRate}) => { |
| 30 | + |
| 31 | + const monthlyPayment = currentBalance * minimumRate; |
| 32 | + const interestPayment = currentBalance * monthlyRate; |
| 33 | + const principalPayment = monthlyPayment - interestPayment; |
| 34 | + |
| 35 | + return { |
| 36 | + |
| 37 | + newBalance: currentBalance - principalPayment, |
| 38 | + monthlyPayment, |
| 39 | + principalPayment |
| 40 | + |
| 41 | + }; |
| 42 | + |
| 43 | +}; |
| 44 | + |
| 45 | + |
| 46 | +const runningBalance = maxMonths => |
| 47 | + ({annualRate, minimumRate, startingBalance}) => { |
| 48 | + |
| 49 | + const runningBalanceRecur = |
| 50 | + ({currentBalance, totalPaid, monthIndex}) => { |
| 51 | + |
| 52 | + if (monthIndex === maxMonths) |
| 53 | + return {currentBalance, totalPaid}; |
| 54 | + |
| 55 | + let {newBalance, monthlyPayment, principalPayment} = |
| 56 | + monthlyFigures ({ |
| 57 | + |
| 58 | + currentBalance, |
| 59 | + monthlyRate: annualRate / 12, |
| 60 | + minimumRate |
| 61 | + |
| 62 | + }); |
| 63 | + |
| 64 | + const numToDollars = numToCurrency ('$'); |
| 65 | + logger.append ('Month: ' + (monthIndex + 1)); |
| 66 | + logger.append ('Minimum monthly payment: ' + |
| 67 | + numToDollars (monthlyPayment)); |
| 68 | + logger.append ('Principle paid: ' + |
| 69 | + numToDollars (principalPayment)); |
| 70 | + logger.append ('Remaining balance: ' + numToDollars (newBalance)); |
| 71 | + |
| 72 | + return runningBalanceRecur ({ |
| 73 | + |
| 74 | + currentBalance: newBalance, |
| 75 | + totalPaid: totalPaid + monthlyPayment, |
| 76 | + monthIndex: monthIndex + 1 |
| 77 | + |
| 78 | + }); |
| 79 | + |
| 80 | + }; |
| 81 | + |
| 82 | + return runningBalanceRecur ({ |
| 83 | + |
| 84 | + currentBalance: startingBalance, |
| 85 | + totalPaid: 0, |
| 86 | + monthIndex: 0 |
| 87 | + |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | + |
| 92 | +const main = annualRate => minimumRate => startingBalance => { |
| 93 | + |
| 94 | + let {currentBalance, totalPaid} = runningBalance (12)({ |
| 95 | + annualRate, minimumRate, startingBalance}); |
| 96 | + |
| 97 | + const numToDollars = numToCurrency ('$'); |
| 98 | + logger.append ('RESULT'); |
| 99 | + logger.append ('Total amount paid: ' + numToDollars (totalPaid)); |
| 100 | + logger.append ('Remaining balance: ' + numToDollars (currentBalance)); |
| 101 | + logger.flush (); |
| 102 | + return true; |
| 103 | + |
| 104 | +}; |
| 105 | + |
| 106 | + |
| 107 | +module.exports = main; |
| 108 | + |
0 commit comments