-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoanCalculator.html
215 lines (194 loc) · 7.9 KB
/
LoanCalculator.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
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Loan Calculator</title>
<style>
.output {font-weight: bold;}
#paymeny {text-decoration: underline;}
#graph {border: solid black 1px;}
th, td {vertical-align: top;}
</style>
</head>
<body>
<table>
<tr>
<th>Enter loan Data:</th>
<td></td>
<th>Loan Balance, Cumulative Equity, and Interest Payment</th>
</tr>
<tr>
<td>Amount of the loan ($):</td>
<td><input id="amount" onchange="calculate();"></td>
<td rowspan="8"><canvas id="graph" width="400" height="250"></canvas></td>
</tr>
<tr>
<td>Annual interest (%):</td>
<td><input id="apr" onchange="calculate();"></td>
</tr>
<tr>
<td>RePayment period (years):</td>
<td><input id="years" onchange="calculate();"></td>
</tr>
<tr>
<td>Zipcode (to find lenders):</td>
<td><input id="zipcode" onchange="calculate();"></td>
</tr>
<tr>
<th>Approximate Payments:</th>
<td><button onclick="calculate();">Calculate</button></td>
</tr>
<tr>
<td>Monthly payment:</td>
<td>$<span class="output" id="payment"></span></td>
</tr>
<tr>
<td>Total payments:</td>
<td>$<span class="output" id="total"></span></td>
</tr>
<tr>
<td>Total interest:</td>
<td>$<span class="output" id="totalinterest"></span></td>
</tr>
<tr>
<th>Sponsors:</th>
<td colspan="2">
Apply for your loan with one of the these fine lenders:
<div id="lenders"></div>
</td>
</tr>
</table>
<script>
"use strict";
function calculate(){
var amount = document.getElementById("amount");
var apr = document.getElementById("apr");
var years = document.getElementById("years");
var zipcode = document.getElementById("zipcode");
var payment = document.getElementById("payment");
var total = document.getElementById("total");
var totalinerest = document.getElementById("totalinterest");
var principal = parseFloat(amount.value);
var interest = parseFloat(apr.value)/100/12;
var payments = parseFloat(years.value)*12;
var x = Math.pow(1+interest, payments);
var monthly = (principal*x*interest)/(x-1);
if(isFinite(monthly)){
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (monthly*payments).toFixed(2);
totalinerest.innerHTML = ((monthly*payments)-principal).toFixed(2);
save(amount.value, apr.value, years.value, zipcode.value);
// try{
// getLenders(amount.value, apr.value, years.value, zipcode.value);
// }catch(e){}
chart(principal, interest, monthly, payments);
}
else{
payment.innerHTML = "";
total.innerHTML = "";
totalinterest.innerHTML = "";
chart();
}
}
function save(amount, apr, years, zipcode){
if(window.localStorage){
localStorage.loan_amount = amount;
localStorage.loan_apr = apr;
localStorage.loan_years = years;
localStorage.loan_zipcode = zipcode;
}
}
window.onload = function(){
if(window.localStorage && localStorage.loan_amount){
document.getElementById("amount").value = localStorage.loan_amount;
document.getElementById("apr").value = localStorage.loan_apr;
document.getElementById("years").value = localStorage.loan_years;
document.getElementById("zipcode").value = localStorage.loan_zipcode;
}
};
function getLenders(amount, apr, years, zipcode){
if(!window.XMLHttpRequest) return;
var ad = document.getElementById("lenders");
if(!ad) return;
var url = "getLenders.php" +
"?amt" + encodeURIComponent(amount)+
"&apr" + encodeURIComponent(apr)+
"&yrs" + encodeURIComponent(years)+
"&zip" + encodeURIComponent(zipcode);
var req = new XMLHttpRequest();
req.open("GET", url);
req.send(null);
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status==200){
var response = req.responseText;
var lenders = JSON.parse(response);
var list = "";
for(var i=0; i<lenders.length; i++){
list+="<li><a href='" +lenders[i].url +"'>" + lenders[i].name + "</a>"
}
ad.innerHTML = "<ul>" +list + "</ul>";
}
}
}
function chart(principal, interest, monthly, payments){
var graph = document.getElementById("graph");
graph.width = graph.width;
if(arguments.length==0 || !graph.getContext) return;
var g = graph.getContext("2d");
var width = graph.width, height = graph.height;
function paymentToX(n) {return n*width/payments;}
function amountToY(a){ return height - (a*height/(monthly*payments*1.05));}
g.moveTo(paymentToX(0), amountToY(0));
g.lineTo(paymentToX(payments), amountToY(monthly*payments));
g.lineTo(paymentToX(payments), amountToY(0));
g.closePath();
g.fillStyle = "#f88";
g.fill();
g.font = "bold 12px sans-serif";
g.fillText("Total Interest Payments", 20,20);
var equity = 0;
g.beginPath();
g.moveTo(paymentToX(0), amountToY(0));
for(var p=1; p<=payments; p++){
var thisMonthsInterest = (principal-equity)*interest;
equity += (monthly - thisMonthsInterest);
g.lineTo(paymentToX(p), amountToY(equity));
}
g.lineTo(paymentToX(payments), amountToY(0));
g.closePath();
g.fillStyle = "green";
g.fill();
g.fillText("Total Equity", 20, 35);
var bal = principal;
g.beginPath();
g.moveTo(paymentToX(0), amountToY(bal));
for(var p=1; p<=payments; p++){
var thisMonthsInterest = bal*interest;
bal -= (monthly - thisMonthsInterest);
g.lineTo(paymentToX(p), amountToY(bal));
}
g.lineWidth = 3;
g.stroke();
g.fillStyle = "black";
g.fillText("Loan Balance", 20, 50);
g.textAlign = "center";
var y = amountToY(0);
for(var year = 1; year*12<=payments; year++){
var x = paymentToX(year*12);
g.fillRect(x-0.5, y-3,1,3);
if(year==1) g.fillText("Year", x, y-5);
if(year%5==0&&year*12!==payments)
g.fillText(String(year), x, y-5);
}
g.textAlign = "right";
g.textBaseline = "middle";
var ticks = [monthly*payments, principal];
var rightEdge = paymentToX(payments);
for(var i=0; i<ticks.length; i++){
var y = amountToY(ticks[i]);
g.fillRect(rightEdge-3, y-0.5, 3, 1);
g.fillText(String(ticks[i].toFixed(0)), rightEdge-5, y);
}
}
</script>
</body>
</html>