-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompound-interest-calculator.pug
423 lines (360 loc) · 17 KB
/
compound-interest-calculator.pug
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
---
title: Compound Interest Calculator
---
h1 Compound Interest Calculator
p Calculating how much money you may have with compound interest is a very interesting tool to know how your savings rate will define your future life. Of course, all values on this page are for entertainment purposes only — know your own risk, and be realistic.
style(type="text/css" media="screen").
#alert,
#withdrawlAlert {
display: block;
width: 100%;
padding: 1rem;
color: #D8000C;
background-color: #FFD2D2;
border-color: #D8000C;
text-align: center;
}
input.numberInput,
input.withdrawlNumberInput {
border: 1px solid black;
background-color: #BDE5F8;
color: #101318;
font-size: 1rem;
padding: .5rem .25rem;
width: 100%;
text-align: right;
}
input.numberInput:read-only,
input.withdrawlNumberInput:read-only {
background-color: #5f6874;
}
form div {
display: flex;
align-items: center;
}
form .inputRow label,
form .inputRow input {
width: 100%;
margin: .5rem;
}
form .inputRow label {
text-align: right;
width: 60%;
}
.rounding {
text-align: center;
}
.rounding .checkboxInput {
padding: 0;
margin: 0;
vertical-align: baseline;
}
@media all and (max-width: 500px) {
form div {
flex-flow: column;
}
form .inputRow label {
text-align: left;
margin-bottom: 0;
width: 100%;
}
}
#results strong {
font-weight: bold;
}
.interestcalculator.paragraph
form
.inputRow
label.calcLabel Initial Investment:
input#investmentInitial.numberInput(type='number' placeholder='Initival Investment' value='1000')
.inputRow
label.calcLabel Monthly Contribution:
input#monthlyContribution.numberInput(type='number' placeholder='Initival Investment' value='100')
.inputRow
label.calcLabel Yearly Contribution:
input#yearlyContribution.numberInput(type='text' placeholder='Initival Investment' value='1200' readonly='')
.inputRow
label.calcLabel Years to Grow:
input#years.numberInput(type='number' max='1000' placeholder='Initival Investment' value='5')
.inputRow
label.calcLabel Estimated Yearly Interest Rate:
input#interestRate.numberInput(type='number' placeholder='Initival Investment' value='7')
#alert(style='display: none;') The values entered are not valid.
#results(style='display: none;')
p(style='text-align: center; font-size: 2rem') In
= ' '
strong#resultYears
= ' '
| years, you will have
= ' '
strong#resultAccumulated
= ' '
| .
ul
li You will have invested a total of
= ' '
strong#resultInvestedTotal
= ' '
| yourself and earned
= ' '
strong#resultInterestEarned
= ' '
| through interest.
li Based on the 4% rule, you could then withdraw aprox.
= ' '
strong#resultWithdrawl
= ' '
| per year (which equals about
= ' '
strong#resultWithdrawlMonthly
= ' '
| ) without running out of money.
#resultTable
script(src="https://cdn.jsdelivr.net/npm/jquery@3.5/dist/jquery.min.js")
script(type="text/javascript").
if (typeof($) === 'undefined') $ = jQuery
$(function() {
console.log('Compound Interest Calculator App');
const $alert = $('#alert')
$alert.hide(true)
const $results = $('#results')
$results.hide(true)
const $inputs = $('.numberInput')
const $investmentInitial = $('#investmentInitial')
const $monthlyContribution = $('#monthlyContribution')
const $yearlyContribution = $('#yearlyContribution')
const $years = $('#years')
const $interestRate = $('#interestRate')
// const $roundResult = $('#roundResult')
const $resultYears = $('#resultYears')
const $resultAccumulated = $('#resultAccumulated')
const $resultInvestedTotal = $('#resultInvestedTotal')
const $resultInterestEarned = $('#resultInterestEarned')
const $resultTable = $('#resultTable')
const $resultWithdrawl = $('#resultWithdrawl')
const $resultWithdrawlMonthly = $('#resultWithdrawlMonthly')
function calcStuff() {
let investmentInitial = parseFloat($investmentInitial.val())
let monthlyContribution = parseFloat($monthlyContribution.val())
let years = parseFloat($years.val())
let interestRate = parseFloat($interestRate.val())
// const roundResult = $roundResult.is(":checked")
$yearlyContribution.val((Math.round(monthlyContribution * 12 * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
if ((parseFloat(investmentInitial) == 0 || parseFloat(investmentInitial) != 0) && (parseFloat(monthlyContribution) == 0 || parseFloat(monthlyContribution) != 0) && parseFloat(years) && parseFloat(interestRate)) {
$alert.hide()
$results.show()
const { accumulated, interestEarnedTotal, accumulatedYears } = compoundInterest(investmentInitial, interestRate, years, 12 * monthlyContribution)
$resultYears.text(years)
$resultAccumulated.text((Math.round(accumulated * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
const investedTotal = investmentInitial + (years * (monthlyContribution * 12))
$resultInvestedTotal.text(investedTotal.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
$resultInterestEarned.text((Math.round((interestEarnedTotal) * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
$resultWithdrawl.text((Math.round((accumulated * 0.04) * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
$resultWithdrawlMonthly.text((Math.round((accumulated * 0.04) / 12 * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
let resultTable = `<div class="tbl-resp" role="table" aria-label="Destinations">
<div class="flex-table header" role="rowgroup">
<div class="flex-row first" role="columnheader">Year</div>
<div class="flex-row" role="columnheader">Before</div>
<div class="flex-row" role="columnheader">Addition</div>
<div class="flex-row" role="columnheader">Interest</div>
<div class="flex-row" role="columnheader">Accumulated</div>
</div>`
for (obj of accumulatedYears) {
resultTable += `<div class="flex-table row" role="rowgroup">
<div class="flex-row first" role="cell">${obj.year}</div>
<div class="flex-row" role="cell">${obj.before.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
<div class="flex-row" role="cell">${obj.addition.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
<div class="flex-row" role="cell">${obj.interestEarned.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
<div class="flex-row" role="cell">${obj.accumulated.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
</div>`
}
resultTable += "</div>"
$resultTable.html(resultTable);
// $megaPixelsMega.text(Math.round(oldWidth*oldHeight/10000)/100).toLocaleString()
} else {
$alert.show()
$results.hide()
}
}
$inputs.on('change paste keyup', calcStuff)
// $roundResult.on('change paste keyup', calcStuff)
calcStuff()
// Input it initial amount
// Interest as a number, e.g. 5% is 1.05 on a yearly basis
// Length as number of years
// Name of this calculation
// Addition determines whether the input variable is one time or a yearly contribution
function compoundInterest(input, interest, length, addition) {
interest = interest / 100
let accumulated = input
let accumulatedBefore = 0
let interestEarned = 0
let interestEarnedTotal = 0
const accumulatedYears = []
for (i = 0; i < length; i++) {
accumulatedBefore = accumulated
interestEarned = accumulated * interest
interestEarnedTotal += interestEarned
accumulated = (addition) ? accumulated + interestEarned + addition : accumulated + interestEarned
const obj = {
year: i + 1,
before: accumulatedBefore,
addition: addition,
interestEarned: interestEarned,
accumulated: accumulated,
}
accumulatedYears.push(obj)
}
return {accumulated, interestEarnedTotal, accumulatedYears}
}
});
h1 Withdrawl Calculator
p This calculator gives you an idea about how your withdrawal could look like.
.withdrawlcalculator.paragraph
form
.inputRow
label.calcLabel Initial Investment:
input#withdrawlInvestmentInitial.withdrawlNumberInput(type='number' placeholder='' value='300000')
.inputRow
label.calcLabel Monthly Payout:
input#withdrawlMonthlyPayout.withdrawlNumberInput(type='number' placeholder='' value='1000')
.inputRow
label.calcLabel Yearly Payout:
input#withdrawlYearlyPayout.withdrawlNumberInput(type='text' placeholder='' value='12000' readonly='')
.inputRow
label.calcLabel Years to Payout:
input#withdrawlYears.withdrawlNumberInput(type='number' max='1000' placeholder='' value='5')
.inputRow
label.calcLabel Estimated Yearly Interest Rate:
input#withdrawlInterestRate.withdrawlNumberInput(type='number' placeholder='' value='7')
#withdrawlAlert(style='display: none;')
| The values entered are not valid.
#withdrawlResults(style='display: none;')
p(style='text-align: center; font-size: 2rem')
| After
= ' '
strong#withdrawlResultYears
= ' '
| years, you will have
= ' '
strong#withdrawlResultAccumulated
= ' '
| .
ul
li
| You will have withdrawn
= ' '
strong#withdrawlResultWithdrawnTotal
= ' '
| and earned a total of
= ' '
strong#withdrawlResultInterestEarned
= ' '
| in interest.
li#withdrawlResultBad With the current values you are earning less interest per year than you are withdrawing. Your money will run out at some point!
li#withdrawlResultGood With the current values you are earning more interest per year than you are withdrawing. Your money will last you forever — stay the course, you are awesome.
#withdrawlResultTable
script(type="text/javascript").
if (typeof($) === 'undefined') $ = jQuery
$(function() {
console.log('Withdrwal Calculator App');
const $alert = $('#withdrawlAlert')
$alert.hide(true)
const $results = $('#withdrawlResults')
$results.hide(true)
const $inputs = $('.withdrawlNumberInput')
const $investmentInitial = $('#withdrawlInvestmentInitial')
const $yearlyPayout = $('#withdrawlYearlyPayout')
const $monthlyPayout = $('#withdrawlMonthlyPayout')
const $years = $('#withdrawlYears')
const $interestRate = $('#withdrawlInterestRate')
const $resultYears = $('#withdrawlResultYears')
const $resultAccumulated = $('#withdrawlResultAccumulated')
const $resultWithdrawnTotal = $('#withdrawlResultWithdrawnTotal')
const $resultInterestEarned = $('#withdrawlResultInterestEarned')
const $resultTable = $('#withdrawlResultTable')
const $resultBad = $('#withdrawlResultBad')
const $resultGood = $('#withdrawlResultGood')
function calcStuff() {
let investmentInitial = parseFloat($investmentInitial.val())
let monthlyPayout = parseFloat($monthlyPayout.val())
let years = parseFloat($years.val())
let interestRate = parseFloat($interestRate.val())
// const roundResult = $roundResult.is(":checked")
$yearlyPayout.val((Math.round(monthlyPayout * 12 * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
if ((parseFloat(investmentInitial) == 0 || parseFloat(investmentInitial) != 0) && (parseFloat(monthlyPayout) == 0 || parseFloat(monthlyPayout) != 0) && parseFloat(years) && parseFloat(interestRate)) {
$alert.hide()
$results.show()
const { accumulated, interestEarnedTotal, accumulatedYears } = compoundInterest(investmentInitial, interestRate, years, (-1 * 12 * monthlyPayout))
$resultYears.text(years)
$resultAccumulated.text((Math.round(accumulated * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
const payoutTotal = (years * (monthlyPayout * 12))
$resultWithdrawnTotal.text(payoutTotal.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
$resultInterestEarned.text((Math.round(interestEarnedTotal * 100) / 100).toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}))
if (interestEarnedTotal > payoutTotal) {
$resultGood.show()
$resultBad.hide()
} else {
$resultBad.show()
$resultGood.hide()
}
let resultTable = `<div class="tbl-resp" role="table" aria-label="Destinations">
<div class="flex-table header" role="rowgroup">
<div class="flex-row first" role="columnheader">Year</div>
<div class="flex-row" role="columnheader">Before</div>
<div class="flex-row" role="columnheader">Withdrawl</div>
<div class="flex-row" role="columnheader">Interest</div>
<div class="flex-row" role="columnheader">Accumulated</div>
</div>`
for (obj of accumulatedYears) {
resultTable += `<div class="flex-table row" role="rowgroup">
<div class="flex-row first" role="cell">${obj.year}</div>
<div class="flex-row" role="cell">${obj.before.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
<div class="flex-row" role="cell">${obj.addition.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
<div class="flex-row" role="cell">${obj.interestEarned.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
<div class="flex-row" role="cell">${obj.accumulated.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
</div>`
}
resultTable += "</div>"
$resultTable.html(resultTable);
} else {
$alert.show()
$results.hide()
}
}
$inputs.on('change paste keyup', calcStuff)
// $roundResult.on('change paste keyup', calcStuff)
calcStuff()
// Input it initial amount
// Interest as a number, e.g. 5% is 1.05 on a yearly basis
// Length as number of years
// Name of this calculation
// Addition determines whether the input variable is one time or a yearly contribution
function compoundInterest(input, interest, length, addition) {
interest = interest / 100
let accumulated = input
let accumulatedBefore = 0
let interestEarned = 0
let interestEarnedTotal = 0
const accumulatedYears = []
for (i = 0; i < length; i++) {
accumulatedBefore = accumulated
interestEarned = accumulated * interest
interestEarnedTotal += interestEarned
accumulated = (addition) ? accumulated + interestEarned + addition : accumulated + interestEarned
const obj = {
year: i + 1,
before: accumulatedBefore,
addition: addition,
interestEarned: interestEarned,
accumulated: accumulated,
}
accumulatedYears.push(obj)
}
return {accumulated, interestEarnedTotal, accumulatedYears}
}
});
div.content.feedback
p Any issues? Email me #[a(href='mailto:team@ChrisSpiegl.com?subject=Compound Interest Calculator Problem') team@ChrisSpiegl.com].
p Would like to support me in creating more tools and services like this?
p: a(href='/patron') Buy me a Tea&Cookie