-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
53 lines (48 loc) · 1.72 KB
/
calculator.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
window.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById("calc-form");
if (form) {
setupIntialValues();
form.addEventListener("submit", function(e) {
e.preventDefault();
update();
});
}
});
function getCurrentUIValues() {
return {
amount: +(document.getElementById("loan-amount").value),
years: +(document.getElementById("loan-years").value),
rate: +(document.getElementById("loan-rate").value),
}
}
// Get the inputs from the DOM.
// Put some default values in the inputs
// Call a function to calculate the current monthly payment
function setupIntialValues() {
document.getElementById("loan-amount").value = 50000;
document.getElementById("loan-years").value = 15;
document.getElementById("loan-rate").value = .14;
loanObj = getCurrentUIValues();
updateMonthly(calculateMonthlyPayment(loanObj));
}
// Get the current values from the UI
// Update the monthly payment
function update() {
updateMonthly(calculateMonthlyPayment(getCurrentUIValues()));
}
// Given an object of values (a value has amount, years and rate ),
// calculate the monthly payment. The output should be a string
// that always has 2 decimal places.
function calculateMonthlyPayment(values) {
let amount = values.amount;
let interestRate = values.rate / 12;
let totalPayments = values.years * 12;
let monthlyPayment = (amount * interestRate) / (1 - ((1 + interestRate)**(-totalPayments)));
monthlyPayment = Math.round(monthlyPayment * 100) / 100;
return monthlyPayment.toString();
}
// Given a string representing the monthly payment value,
// update the UI to show the value.
function updateMonthly(monthly) {
document.getElementById("monthly-payment").innerText = monthly;
}