-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
78 lines (59 loc) · 2.25 KB
/
App.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
//Listen for submit
document.getElementById("loan-form").addEventListener("submit", function (e) {
//Hide Results
document.getElementById("results").style.display = "none";
//Show Loader
document.getElementById("loading").style.display = "block";
setTimeout(calculateResults, 2000);
e.preventDefault();
});
//Calculate Result
function calculateResults() {
console.log("calculating");
//UI Variables
const amount = document.getElementById("amount");
const interestRate = document.getElementById("interest-rate");
const months = document.getElementById("months");
const monthlyPayment = document.getElementById("monthly-payment");
const totalDue = document.getElementById("total-due");
const interest = document.getElementById("Interest");
const principal = parseFloat(amount.value);
const calculatedInterest = parseFloat(interestRate.value) / 100 / 12;
const calculatedPayments = parseFloat(months.value);
//Monthly Payments
const x = Math.pow(1 + calculatedInterest, calculatedPayments);
const monthly = (principal * x * calculatedInterest) / (x - 1);
if (isFinite(monthly)) {
monthlyPayment.value = monthly.toFixed(2);
totalDue.value = (monthly * calculatedPayments).toFixed(2);
interest.value = (monthly * calculatedPayments - principal).toFixed(2);
//Show results
document.getElementById("results").style.display = "block";
//Hide Spinner
document.getElementById("loading").style.display = "none";
} else {
showError("Please check the numbers you have used.");
}
}
function showError(error) {
//Hide results
document.getElementById("results").style.display = "none";
//Hide Spinner
document.getElementById("loading").style.display = "none";
//Create a div
const errorDiv = document.createElement("div");
//Get elements
const card = document.querySelector(".card");
const heading = document.querySelector(".heading");
//Add class for bootstrap
errorDiv.className = "alert alert-danger";
//Create text node and append to div
errorDiv.appendChild(document.createTextNode(error));
//Insert error above card
card.insertBefore(errorDiv, heading);
//Clear error after 5 seconds
setTimeout(clearError, 5000);
}
function clearError() {
document.querySelector(".alert").remove();
}