-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (58 loc) · 2.35 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
const amt = document.querySelector('#loan-amount'),
rate = document.querySelector('#interest-rate'),
duration = document.querySelector('#duration'),
loanform = document.querySelector("#loan-form"),
mthlyInt = document.querySelector('.mthly-int-amt'),
totalInt = document.querySelector('.total-int-amt'),
totalloan = document.querySelector('.total-loan-sum'),
innertopContainer = document.querySelector('.inner-top'),
bottomContainer = document.querySelector('.btm'),
loader = document.querySelector('.loader');
let alertCount = 0;
bottomContainer.style.display = 'none';
loadEventListeners();
function loadEventListeners(){
loanform.addEventListener("submit",calculateLoan);
}
function calculateLoan(e){
var missingField = document.createElement('div');
missingField.id = "alert-field";
missingField.className = "redalert row justify-content-center";
missingField.innerHTML = '<div class="alert alert-danger col-sm-3">Enter all fields</div>';
const alert = document.querySelector('.redalert');
if (amt.value === '' || rate.value === '' || duration.value === ''){
if (alertCount === 0){
innertopContainer.insertBefore(missingField,innertopContainer.childNodes[0]);
alertCount += 1;
}
else{
alert.style.display = "";
}
}else{
if (alertCount > 0){
alert.style.display = "none";
}
loader.style.display = "";
bottomContainer.style.display = "none";
setTimeout(timer, 2000);
mthlyInt.value = "$";
mthlyInt.value += calculateMthlyInt();
totalInt.value = "$"
totalInt.value += (parseFloat(calculateMthlyInt()) * duration.value * 12).toFixed(2);
totalloan.value = "$"
totalloan.value += ((parseFloat(calculateMthlyInt()) * duration.value * 12) + parseFloat(amt.value)).toFixed(2);
}
e.preventDefault();
}
function calculateMthlyInt(){
// calculate interest rate per month
var R = rate.value / (12 * 100);
// calculate mthly interest
var x = (amt.value * R * Math.pow((1 + R),(duration.value*12))) / Math.pow((1+R),((duration.value*12)-1));
mthlyInterestAmt = parseFloat(x).toFixed(2);
return mthlyInterestAmt;
}
function timer(){
loader.style.display = "none"
bottomContainer.style.display = "block";
};