-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompoundInterest.ts
126 lines (105 loc) · 3.55 KB
/
compoundInterest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { getNewDataMap, getRequiredPaddingLength } from "./dataMap";
import { addAnnualReturn, padRight } from "./functions";
import { roundFormat } from "./currencyFormat";
import { DepositFrequency } from "./enums";
const getCompoundInterest = (
yearsCount: number,
annualReturnPercentage: number,
depositAmount: number,
depositRate: DepositFrequency
) => {
let yearIndex = 1;
const oneTimeDeposit = () => depositRate === DepositFrequency.OneTime;
const monthlyDeposit = () => depositRate === DepositFrequency.Monthly;
const firstYear = () => yearIndex === 1;
const map = getNewDataMap();
for (; yearIndex <= yearsCount; ++yearIndex) {
const lastYearIndex = yearIndex - 1;
const currentYearDeposit = oneTimeDeposit()
? firstYear()
? depositAmount
: 0
: monthlyDeposit()
? depositAmount * 12
: depositAmount;
const lastYearInfo = map.get(lastYearIndex);
if (lastYearInfo === undefined) {
console.log("Error", "lastYearInfo is undefined");
return;
}
const lastYearDeposit = lastYearInfo.totalDeposit;
const totalDeposit =
lastYearDeposit +
(oneTimeDeposit()
? firstYear()
? depositAmount
: 0
: currentYearDeposit);
const totalMoney = addAnnualReturn(
lastYearInfo.totalMoney + currentYearDeposit,
annualReturnPercentage
);
const totalProfit = totalMoney - totalDeposit;
const monthlyProfit = totalProfit / yearIndex / 12;
map.set(yearIndex, {
totalDeposit,
totalMoney,
totalProfit,
monthlyProfit,
});
}
return map;
};
const printCompoundInterest = (
dataMap: Map<
number,
{
totalDeposit: number;
totalMoney: number;
totalProfit: number;
monthlyProfit: number;
}
>,
depositRate: DepositFrequency
) => {
const alignWithSpaces = (str: string): string =>
padRight(str, getRequiredPaddingLength(dataMap));
const roundAlignFormat = (value: number): string =>
alignWithSpaces(roundFormat(value));
const translatedData = Array.from(dataMap.entries()).map(
([, value], index) => ({
Year: index,
Invested: roundAlignFormat(value.totalDeposit),
Have: roundAlignFormat(value.totalMoney),
Profit: roundAlignFormat(value.totalProfit),
"Monthly profit": roundAlignFormat(value.monthlyProfit),
})
);
translatedData.shift();
const firstYearInfo = dataMap.get(1);
if (firstYearInfo === undefined) {
console.log("Error", "firstYearInfo is undefined");
return;
}
const annualReturnPercentage = Math.round(
(100 * firstYearInfo.totalProfit) / firstYearInfo.totalDeposit
);
const oneTimeDeposit = () => depositRate === DepositFrequency.OneTime;
const yearlyDeposit = () => depositRate === DepositFrequency.Yearly;
console.log("Total Years of Investment:", dataMap.size - 1);
console.log("Annual Return Percentage:", annualReturnPercentage);
if (oneTimeDeposit()) {
console.log("One Time Deposit:", firstYearInfo.totalDeposit);
} else if (yearlyDeposit()) {
console.log("Yearly Deposit:", firstYearInfo.totalDeposit);
} else {
console.log("Monthly Deposit:", firstYearInfo.totalDeposit / 12);
}
console.log(
`All table values are cumulative -
- ‘Invested’/‘Have’/‘Profit’ in total. Not per year/row.
- ‘Monthly profit’ is calculated by dividing the total profit accumulated up to the specified year by the number of months from the start until that year.`
);
console.table(translatedData);
};
export { getCompoundInterest, printCompoundInterest };