-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
76 lines (69 loc) · 2.56 KB
/
app.js
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
const fs = require('fs');
const helpers = require('./helpers');
const tableHeader = helpers.tableHeader;
const investmentObj = {
currentInvestment: 3000,
interestRate: 13.14,
investmentTerm: 25,
currentAge: 25,
annualIncrement: 1000,
currency: 'INR'
}
function calculateSip() {
let carryForwardAmount = 0;
let updatedInvestmentObj;
let totalInvestment;
let investmentArr = [];
for (i = 1; i <= investmentObj.investmentTerm; i++) {
if (investmentObj.annualIncrement && (i > 1)) {
investmentObj.currentInvestment = investmentObj.currentInvestment + investmentObj.annualIncrement;
totalInvestment = investmentObj.currentInvestment * 12
} else {
totalInvestment = investmentObj.currentInvestment * convertToMonths(i);
}
const interestEarned = Math.floor((carryForwardAmount + totalInvestment) * investmentObj.interestRate / 100);
const yearEndAccumulation = totalInvestment + interestEarned + carryForwardAmount;
investmentObj.currentAge = i > 1 ? (investmentObj.currentAge + 1) : investmentObj.currentAge;
updatedInvestmentObj = {
...investmentObj,
term: i,
totalInvestment: totalInvestment,
interestEarned: interestEarned,
carryForwardAmount: carryForwardAmount,
corpusWithCAGR: yearEndAccumulation,
age: investmentObj.currentAge
}
investmentArr.push(updatedInvestmentObj);
carryForwardAmount = yearEndAccumulation;
}
constructTableBody(investmentArr);
}
function getUnit(value){
let leng = value.toString().length;
let unit = leng == 6 ? ' lakh' : leng == 7 ? ' lakh': leng == 8 ? 'Cr' : leng == 9 ? 'Cr' : ''
return `${putCommas(value) + unit}`
}
function convertToMonths(years) {
return years * 12;
}
function putCommas(amount) {
return amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function writeToFile(tableBody){
const fileName = `${investmentObj.currentInvestment}_at_${investmentObj.interestRate}_interest.md`;
fs.writeFile(fileName, tableBody, function(err) {
if (err) {
return console.log(err);
}
console.log(`Investment calculated`);
});
}
function constructTableBody(investmentArr) {
let tableBody = [];
investmentArr.map((x, i) => {
tableBody.push(`\n | ${x.term} | ${x.age} | ${getUnit(x.carryForwardAmount)} | ${putCommas(x.currentInvestment)} | ${x.interestRate} | ${getUnit(x.totalInvestment)} | ${'+' + getUnit(x.interestEarned)} | ${getUnit(x.corpusWithCAGR)} | ${(x.term > 1) ? x.annualIncrement : 0} |`)
})
const fullTable = tableHeader.concat(tableBody.join(' '));
writeToFile(fullTable);
}
calculateSip();