-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (49 loc) · 1.82 KB
/
Copy pathscript.js
File metadata and controls
52 lines (49 loc) · 1.82 KB
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
var tY = document.getElementById("totalYears");
var y = document.getElementById("inputs-yearSave");
var p = document.getElementById('inputs-startPrice');
var dollars = document.getElementById('dollars');
var r = document.getElementById("inputs-returnRate");
var f = document.getElementById('frequency');
var c = document.getElementById('inputs-addContribute');
y.addEventListener('input', changePrice);
p.addEventListener('input', changePrice);
r.addEventListener('input', changePrice);
f.addEventListener('change', changePrice);
c.addEventListener('input', changePrice);
function changePrice() {
// Format check so we don't break the display
if (p.value > 999999999 || p.value === "") {
p.value = 0;
}
if (r.value > 50 || r === "") {
r.value = 0;
}
if (y.value > 100) {
y.value = 0;
}
if (y.value > 0 && y.value <= 100) {
tY.innerText = y.value;
}
if (c.value === "") {
c.value = 0;
}
// Calculate balance total with no interest
if (r.value === "" || r.value === 0) {
// Calculate compound interest for principal amount
let balance = Math.round(parseInt(p.value, 10) + (y.value * (c.value * f.value)));
// Update and display balance in USD formatting
dollars.innerText = balance.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
} else {
// Calculate future value of a series
let balance = Math.round(parseInt(p.value) * (Math.pow((1 + ((r.value / 100) / f.value)), (f.value * y.value))) * 100) / 100 +
(parseInt(c.value)) * ((Math.pow((1 + ((r.value / 100) / f.value)), (f.value * y.value)) - 1) / ((r.value / 100) / f.value));
// Update and display balance in USD formatting
dollars.innerText = balance.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
});
}
}