-
Notifications
You must be signed in to change notification settings - Fork 3
/
compoundInterest.js
118 lines (93 loc) · 3.82 KB
/
compoundInterest.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
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
// add event handler to form submit
document.getElementById("calculator-form").addEventListener("submit", handleSubmit);
// event handler for form submit
function handleSubmit(e) {
e.preventDefault();
// clear old results
removeTable();
// get the values of all the form fields
let initialBalance = document.getElementById("initial-balance").value;
let interestRate = document.getElementById("interest-rate").value;
let calculationPeriod = document.getElementById("calculation-period").value;
// check for errors in values
if(checkValuesInRange(initialBalance, interestRate, calculationPeriod)){
let results = calculateInterest(initialBalance, interestRate, calculationPeriod);
document.getElementById("spinner").style.visibility = "visible";
setTimeout(displayResults, 1500, results);
console.log(results);
}
}
// checks there are no errored values in the input form
function checkValuesInRange(initBal, intRate, calcPeriod){
if(!initBal || !intRate || !calcPeriod){
displayError();
return false;
}
if(initBal < 0 || intRate < 0 || calcPeriod < 0){
displayError();
return false;
}
return true;
}
// calculates compound interest, returns a table of results
function calculateInterest(initBal, intRate, calcPeriod){
let table = {
year: [],
yearInterest: [],
totalInterest: [],
balance: []
};
// pushing first year (no calculations needed)
table.year.push(0);
table.yearInterest.push(0);
table.totalInterest.push(0);
table.balance.push(initBal);
// calculates the balance, total interest and yearly interest rounded to 2 d.p.
for(let i=1;i<=calcPeriod;i++){
table.year.push(i);
table.balance.push(Math.round(100*(initBal * Math.pow((1 + (intRate/100)), i)))/100);
table.totalInterest.push(Math.round(100*(table.balance[i] - initBal))/100);
table.yearInterest.push(Math.round(100*(table.totalInterest[i] - table.totalInterest[i-1]))/100);
}
return table;
}
// displays the results on the webpage
function displayResults(results){
document.getElementById("spinner").style.visibility = "hidden";
for(let i=0;i<results.year.length;i++){
const row = document.createElement("tr");
const cols = [];
for(let j=0;j<4;j++){
cols.push(document.createElement("td"));
}
cols[0].appendChild(document.createTextNode(`${results.year[i]}`));
cols[1].appendChild(document.createTextNode(`£${results.yearInterest[i]}`));
cols[2].appendChild(document.createTextNode(`£${results.totalInterest[i]}`));
cols[3].appendChild(document.createTextNode(`£${results.balance[i]}`));
for(let k=0;k<cols.length;k++){
row.appendChild(cols[k]);
}
document.querySelector("table").style.visibility = "visible";
document.querySelector("tbody").appendChild(row);
}
}
// shows an error below the form for 3 seconds
function displayError() {
const alert = document.createElement('div');
alert.className = 'alert alert-danger py-1 mt-2';
alert.id = 'alert';
alert.appendChild(document.createTextNode('Please fill in all fields correctly.'));
document.querySelector('div.form-group').appendChild(alert);
setTimeout(removeError, 3000);
}
function removeError() {
document.getElementById('alert').outerHTML = '';
}
// removes the body the HTML table and replaces it with a blank one.
function removeTable() {
document.querySelector("tbody").parentNode.removeChild(document.querySelector("tbody"));
const newTable = document.createElement("tbody");
newTable.style = "text-align: center;";
document.querySelector("table").appendChild(newTable);
document.querySelector("table").style.visibility = "hidden";
}