-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
63 lines (54 loc) · 1.81 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
54
55
56
57
58
59
60
61
62
63
window.addEventListener("DOMContentLoaded", function () {
let 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() {
let value = { amount: 20000, years: 8, rate: 2.8 };
let loanAmount = document.getElementById("loan-amount");
loanAmount.value = value.amount;
let termInYears = document.getElementById("loan-years");
termInYears.value = value.years;
let yearlyRate = document.getElementById("loan-rate");
yearlyRate.value = value.rate;
update();
}
// Get the current values from the UI
// Update the monthly payment
function update() {
let uiValues = getCurrentUIValues();
updateMonthly(calculateMonthlyPayment(uiValues));
}
// 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 monthlyRate = values.rate;
let monthlyEquation = monthlyRate / 100 / 12;
let n = Math.floor(values.years * 12);
return (
(monthlyEquation * values.amount) /
(1 - Math.pow(1 + monthlyEquation, -n))
).toFixed(2);
}
// Given a string representing the monthly payment value,
// update the UI to show the value.
function updateMonthly(monthly) {
let monthlyUI = document.getElementById("monthly-payment");
monthlyUI.innerText = "$" + monthly;
}