-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
221 lines (202 loc) · 10 KB
/
index.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compound Interest Calculator</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.min.js"></script>
<link rel="stylesheet" href="style.css">
<script data-ad-client="ca-pub-5132941816670125" async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</head>
<body>
<h1>Compound Interest Calculator</h1>
<section id="input-form" class="main-entry">
<form>
<div>
<label for="initial-value">Enter your starting contribution: $</label>
<input type="number" min="0" step="0.01" placeholder="0.00" value="0.00" onfocus="this.value=''"
id="initial-value" required>
</div>
<div>
<label for="monthly-contribution">Enter your monthly contribution: $</label>
<input type="number" step="0.01" placeholder="0.00" value="0.00" onfocus="this.value=''"
id="monthly-contribution" required>
</div>
<div>
<label for="interest-rate">Enter the estimated annual return of your investments:</label>
<input type="number" min="0" step="0.01" placeholder="9.78" value="9.78" onfocus="this.value=''"
id="interest-rate" required>
<label for="interest-rate">%</label>
</div>
<div>
<label for="num-months">Enter the number of months you'd like to estimate:</label>
<input type="number" min="1" max="1200" placeholder="120" value="120" onfocus="this.value=''"
id="num-months" required>
</div>
<div>
<label for="contribution-increase-pct">Enter your annual increase to monthly contribution:</label>
<input type="number" step="0.01" placeholder="0" value="0" onfocus="this.value=''"
id="contribution-increase-pct" required>
<label for="contribution-increase-pct">%</label>
</div>
<div><input type="button" class="btn btn-primary" value="Calculate!" onclick="plotChart()"
id="submit-button">
</div>
</form>
</section>
<p class="final-value" id="final-value"></p>
<canvas id="finance-chart" width="8" height="2"></canvas>
<script>
const maxYears = 1200;
const defaultMonths = 120;
const defaultInitialContribution = 0;
const defaultMonthlyContribution = 0;
const defaultRateOfReturnPct = 9.78;
const defaultAnnualContributionIncrease = 0;
var myChart;
function getYearsAndMonths(totalMonths) {
return [Math.trunc(totalMonths / 12), totalMonths % 12];
}
function getYearString(months) {
const [numYears, remainingMonths] = getYearsAndMonths(months);
return 'Year ' + numYears + ' Month ' + remainingMonths;
}
function getFinalValueString(finalValue, years, months) {
var yearStr;
var monthStr;
switch (years) {
case 0:
yearStr = "";
break;
case 1:
yearStr = "1 year";
break;
default:
yearStr = years + " years";
}
switch (months) {
case 0:
monthStr = "";
break;
case 1:
monthStr = "1 month";
break;
default:
monthStr = months + " months";
}
if (years >= 1) {
if (months == 0) {
return "After " + yearStr + " you would have: <b class='dollars'>" + new Intl.NumberFormat('en-EN', { style: 'currency', currency: 'USD' }).format(finalValue.toFixed(2)) + "</b>";
} else {
return "After " + yearStr + " and " + monthStr + ", you would have: <b class='dollars'>" + new Intl.NumberFormat('en-EN', { style: 'currency', currency: 'USD' }).format(finalValue.toFixed(2)) + "</b>";
}
} else {
return "After " + monthStr + ", you would have: <b class='dollars'>" + new Intl.NumberFormat('en-EN', { style: 'currency', currency: 'USD' }).format(finalValue.toFixed(2)) + "</b>";
}
}
function generateData(months) {
months = months > maxYears ? maxYears : months;
months += 1;
var monthlyContribution = parseFloat(document.getElementById('monthly-contribution').value) || defaultMonthlyContribution;
const monthlyInterestRate = (parseFloat(document.getElementById('interest-rate').value) || defaultRateOfReturnPct) / 1200;
const annualIncreasePct = (parseFloat(document.getElementById('contribution-increase-pct').value || defaultAnnualContributionIncrease) / 100);
var curContributions = parseFloat(document.getElementById('initial-value').value) || defaultInitialContribution;
var curWealth = document.getElementById('initial-value').value;
var curInvestmentIncome = 0;
var labels = [];
var investmentIncome = [];
var contributions = [];
var i;
for (i = 0; i < months; i++) {
if (i != 0 && i % 12 == 0) {
monthlyContribution *= (1 + annualIncreasePct);
}
labels.push(i);
investmentIncome.push(curInvestmentIncome);
contributions.push(curContributions);
curInvestmentIncome += curWealth * monthlyInterestRate;
curContributions += monthlyContribution;
curWealth = curWealth * (1 + monthlyInterestRate);
curWealth += monthlyContribution;
}
return [contributions, investmentIncome, labels];
}
function plotChart() {
const months = parseInt(document.getElementById('num-months').value) || defaultMonths;
const [contributions, investmentIncome, labels] = generateData(months);
const ctx = document.getElementById('finance-chart').getContext('2d');
if (myChart) {
myChart.data.labels = labels;
myChart.data.datasets[0].data = contributions;
myChart.data.datasets[1].data = investmentIncome;
myChart.update();
} else {
myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: 'Total Contributions',
data: contributions,
stack: 1,
backgroundColor: "#af4bce",
borderWidth: 1
},
{
label: 'Total Investment Return',
data: investmentIncome,
stack: 1,
backgroundColor:
"#ea7369",
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value, index, values) {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value);
}
},
stacked: true
}]
},
tooltips: {
mode: 'x',
intersect: true,
callbacks: {
afterTitle: function () {
window.total = 0;
},
title: function (tooltipItems, data) {
return getYearString(tooltipItems[0].index);
},
label: function (tooltipItem, data) {
var cur = data.datasets[tooltipItem.datasetIndex].label;
var val = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
window.total += val;
return cur + ": " + new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(val.toFixed(2));
},
footer: function () {
return "Total: " + new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(window.total.toFixed(2));
}
}
}
}
});
}
const finalValueContainer = document.getElementById('final-value');
const [totalYears, remainingMonths] = getYearsAndMonths(months);
const finalValue = contributions[contributions.length - 1] + investmentIncome[investmentIncome.length - 1];
finalValueContainer.innerHTML = getFinalValueString(finalValue, totalYears, remainingMonths);
}
</script>
</body>
</html>