-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
85 lines (61 loc) · 2.13 KB
/
script.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
79
80
81
82
83
84
85
document.querySelector('.loan-form').addEventListener('submit',result);
// document.querySelector('.loan-form').addEventListener('submit',showLoading);
//loading function
function showLoading(){
let loading=document.querySelector('.image');
let result=document.querySelector('.calculate-result');
loading.style.display='block';
result.style.display='none';
setTimeout(clearLoading,2000);
// showResult();
}
//clear loading function
function clearLoading(){
let loading=document.querySelector('.image');
loading.style.display='none';
}
function showResult(){
// showLoading();
let result=document.querySelector('.calculate-result');
// result.style.display='block';
//user inputs
let loanAmount=document.getElementById('loan-amount');
let interest = document.getElementById('interest');
let years=document.getElementById('years-to-repay');
//result inputs
let monthlyPayment=document.getElementById('monthly-payment');
let totalPayment=document.getElementById('total-payment');
let totalInterest=document.getElementById('total-interest');
let principal=parseFloat(loanAmount.value);
let calculatedInterest=parseFloat(interest.value)/100/12;
let calculatedPayments=parseFloat(years.value)*12;
//computing monthly payments
let x=Math.pow(1+calculatedInterest, calculatedPayments);
let monthly=(principal*x*calculatedInterest)/(x-1);
if(isFinite(monthly)) {
result.style.display='block';
monthlyPayment.value=monthly.toFixed(2);
totalInterest.value=((monthly * calculatedPayments)-principal).toFixed(2);
totalPayment.value=(monthly*calculatedPayments).toFixed(2);
}
else{
showError()
}
// e.preventDefault();
}
function showError(){
let alert=document.querySelector('.error');
alert.style.display='block';
setTimeout(clearError,3000);
}
//function to clear the error
function clearError() {
let alert=document.querySelector('.error');
alert.style.display='none';
}
//result function
function result(e){
showLoading();
setTimeout(showResult,2000);
e.preventDefault();
}