-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.html
290 lines (219 loc) · 10.8 KB
/
code.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
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
<!doctype html>
<html lang="en" class="h-100">
<head>
<title>Loan Wise: A Coding Project by Murilo Barbosa</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
<script src="https://kit.fontawesome.com/5db21ba9c6.js" crossorigin="anonymous"></script>
<link href="/css/site.css" rel="stylesheet">
<link href="/css/prism.css" rel="stylesheet">
<link rel="icon" type="image/svg" href="img/rect2596.png">
</head>
<body class="d-flex flex-column h-100">
<!-- ++++++++ NAV SECTION ++++++++++++ -->
<nav class="navbar navbar-expand-md navbar-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="index.html"><img src="img/mark3a.svg"
class="d-inline-block align-text-top pb-1" width="35" height="30"> Loan Wise</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse"
aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="app.html">The App</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="code.html">The Code</a>
</li>
<li class="nav-item">
<a class="nav-link" target="_blank" href="https://github.com/murilodab/LoanWise">Git Repo</a>
</li>
<li class="nav-item">
<a class="nav-link" target="_blank" href="https://murilo-barbosa.netlify.app/">About</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- ++++++++ MAIN SECTION ++++++++++++ -->
<main class="flex-shrink-0">
<div class="container py-5 px-5 mt-5">
<h2 class="border-1 border-bottom border-dark">The Code for Loan Wise</h2>
<div class="row row-cols-1 row-cols-lg-2">
<div class="col-lg-8">
<pre class="line-numbers"><code class="language-javascript">
//Fetch values from the page
//starter
function getValues() {
let loanAmount = document.getElementById("loanAmount").value;
let payments = document.getElementById("payments").value;
let rate = document.getElementById("rate").value;
//return object to return multiple attributes
let returnObj = calculateLoan(loanAmount, payments, rate);
displayValues(returnObj);
}
//Calculate payment,principal,interest,total interest,balance
function calculateLoan(loanAmount, months, rate) {
let returnObj = {};
let totalMonthlyPayment = loanAmount * (rate / 1200) / (1 - (1 + rate / 1200) ** (-months));
// ---------- Table Columns ---------
let month = [];
let payment = [];
let principal = [];
let interest = [];
let totalInterest = [];
let balance = [];
//-------------------------------------
let remainingBalance = loanAmount;
let totalInt = 0;
let interestPayment = 0;
let principalPayment = totalMonthlyPayment - interestPayment;
for (let index = 1; index <= months; index++) {
interestPayment = /* previous remaining balance */remainingBalance * (rate / 1200);
principalPayment = totalMonthlyPayment - interestPayment;
remainingBalance = remainingBalance - principalPayment;
totalInt += interestPayment;
//write values to the arrays
payment.push(totalMonthlyPayment);
principal.push(principalPayment);
interest.push(interestPayment);
totalInterest.push(totalInt);
balance.push(remainingBalance);
month.push(index);
}
//Rounding up values
for (let index = 0; index < months; index++) {
payment[index] = payment[index].toFixed(2);
principal[index] = principal[index].toFixed(2);
interest[index] = interest[index].toFixed(2);
totalInterest[index] = totalInterest[index].toFixed(2);
balance[index] = balance[index].toFixed(2);
}
returnObj.month = month;
returnObj.payment = payment;
returnObj.principal = principal;
returnObj.interest = interest;
returnObj.totalInterest = totalInterest;
returnObj.balance = balance;
return returnObj;
}
//Display
function displayValues(returnObj) {
//get the table body element from the page
let tableBody = document.getElementById("results");
//get the table template
let templateRow = document.getElementById("tableTemplate");
//clear the table
tableBody.innerHTML = "";
//Display table on the page using the template
for (let i = 0; i < returnObj.month.length; i++) {
let tableRow = document.importNode(templateRow.content, true);
let rowCols = tableRow.querySelectorAll("td");
rowCols[0].textContent = returnObj.month[i];
rowCols[1].textContent = returnObj.payment[i];
rowCols[2].textContent = returnObj.principal[i];
rowCols[3].textContent = returnObj.interest[i];
rowCols[4].textContent = returnObj.totalInterest[i];
rowCols[5].textContent = returnObj.balance[i];
tableBody.appendChild(tableRow);
}
//fomratting currency values
let USDollar = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
//Display main Values on top
let i = returnObj.month.length - 1; //last index
let intMonthlyPayment = returnObj.payment[i];
let totalPrincipal = document.getElementById("loanAmount").value;
totalPrincipal = parseInt(totalPrincipal);
let totalInterest = returnObj.totalInterest[i];
totalInterest = parseInt(totalInterest);
let totalCost = totalPrincipal + totalInterest;
//writes to the page
document.getElementById("monthlyPayments").innerHTML = `${USDollar.format(intMonthlyPayment)}`;
document.getElementById("totalPrincipal").innerHTML = `${USDollar.format(totalPrincipal)}`;
document.getElementById("totalInterest").innerHTML = `${USDollar.format(totalInterest)}`;
document.getElementById("totalCost").innerHTML = `${USDollar.format(totalCost)}`;
}
</code>
</pre>
</div>
<div class="col-lg-4">
<p>The code is sctructured in Three functions.</p>
<h5>getValues</h5>
<p>The getValues() function plays a crucial role in the loan calculator program. It
serves as the entry point for fetching user input from the webpage, triggering the loan
calculation, and then displaying the results. It retrieves the
values from specific HTML elements on the page.
</p>
<h5>calculateLoan</h5>
<p>The calculateLoan() function takes the loan amount, number of payments (months), and interest
rate as input and calculates the monthly payment, principal, interest, total interest paid, and
remaining balance for each month. It then stores these values in separate arrays. </p>
<p>In this code, returnObj is an object that is used to store and organize the calculated loan
details. It acts as a container to hold multiple attributes related to the loan calculation, and
it allows the functions to return all these attributes together as a single unit.</p>
<h5>displayValues</h5>
<p>The displayValues() function takes the calculated values (stored in the returnObj) and displays
them on the page in a table format. It also calculates and displays the total monthly payment,
total principal amount, total interest paid, and the total cost of the loan.
</p>
<p>The code uses the Intl.NumberFormat object to format the currency values in USD (US dollars)
before displaying them on the page.</p>
</div>
</div>
</div>
</main>
<!-- ++++++++ FOOTER SECTION ++++++++++++ -->
<footer class="footer mt-auto py-1">
<div class="container-fluid">
<div class="row row-cols-1 row-cols-lg-3 gy-2">
<div class="col col-lg-5 order-last order-lg-first text-light justify-content-start pt-2">
<div><span class="text-muted">©2023 Murilo Barbosa | muriloab96@gmail.com</span></div>
</div>
<div class="logo col-12 col-lg-5 d-flex pt-1 pb-1 align-items-center">
<img src="/img/whitelogo8.svg" alt="Murilo Barbosa Logo" height="26">
</div>
<div class="col-4 col-lg-2 d-flex align-items-center justify-content-start justify-content-lg-end pt-1">
<div class="row">
<div class="col-6 social">
<a class="icon-link" href="https://www.linkedin.com/in/murilo-barbosa-55ab1492/"
target="_blank"><i class="fab fa-linkedin fa-2x"></i>
</a>
</div>
<div class="col-6 social">
<a class="icon-link" href="https://github.com/murilodab" target="_blank"><i
class="fab fa-github fa-2x"></i>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</footer>
<!-- Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous">
</script>
<script src="js/prism.js"></script>
<script>
Prism.plugins.NormalizeWhitespace.setDefaults({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true
})
</script>
</body>
</html>