-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (57 loc) · 2.17 KB
/
app.js
File metadata and controls
75 lines (57 loc) · 2.17 KB
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
// all UI value
const amount = document.getElementById('amount');
const interest = document.getElementById('interest');
const year = document.getElementById('years');
const monthlyPayment = document.getElementById('monthly-payment');
const totalPayment = document.getElementById('total-payment');
const totalInterest = document.getElementById('total-interest');
const calculateInterest = document.getElementById('loan-form');
const loader = document.getElementById('loading');
const results = document.querySelector('#results');
const card2 = document.querySelector('.card2');
// Show error
const showError = function(error){
//show loader
loader.style.display = 'none';
card2.style.backgroundColor = "transparent";
const errorDiv = document.createElement('div');
const card = document.querySelector('.card1');
const heading = document.querySelector('.heading');
errorDiv.className = 'alert alert-danger mb-3';
errorDiv.appendChild(document.createTextNode(error));
card.insertBefore(errorDiv, heading);
setTimeout(function(){
document.querySelector('.alert').remove();
},3000)
}
// calculate function
const result = function(){
const principal = parseFloat(amount.value);
const calcInterest = parseFloat(interest.value) / 100 / 12;
const calcPayment = parseFloat(year.value) * 12;
//compute monthly payment
const x = Math.pow(1 + calcInterest, calcPayment);
const monthly = (principal * x * calcInterest) / (x -1);
if(isFinite(monthly)){
monthlyPayment.value = monthly.toFixed(2);
totalPayment.value = (monthly * calcPayment).toFixed(2);
totalInterest.value = ((monthly * calcPayment) - principal).toFixed(2);
//hide result
results.style.display = 'block';
//show loader
loader.style.display = 'none';
}
else{
showError('Input valid Amount');
}
}
// calculate event listener
calculateInterest.addEventListener('submit', function(e){
e.preventDefault();
card2.style.backgroundColor = "#fff";
//hide result
results.style.display = 'none';
//show loader
loader.style.display = 'block';
setTimeout(result, 2000)
});