| 13 | }); |
| 14 | //Calculate Results function |
| 15 | function calculateResults(e){ |
| 16 | console.log('calculating...'); |
| 17 | //UI cars |
| 18 | const ELamount = document.getElementById('amount'); |
| 19 | const ELinterest = document.getElementById('interest'); |
| 20 | const ELyears = document.getElementById('years'); |
| 21 | const ELMonthly_payment = document.getElementById('monthly-payment'); |
| 22 | const ELtotal_payment = document.getElementById('total-payment'); |
| 23 | const ELtotal_interest = document.getElementById('total-interest'); |
| 24 | const principal = parseFloat(ELamount.value); |
| 25 | const calculatedInterest = parseFloat(ELinterest.value) /100 /12; |
| 26 | const calculatedPayment = parseFloat(ELyears.value )*12; |
| 27 | //Create monthly payment |
| 28 | const x = Math.pow(1 + calculatedInterest, calculatedPayment); |
| 29 | const monthly = (principal * x * calculatedInterest) / (x - 1); |
| 30 | console.log(monthly); |
| 31 | if(isFinite(monthly)){//check whether it is finite or not |
| 32 | ELMonthly_payment.value = monthly.toFixed(2); |
| 33 | ELtotal_payment.value = (monthly*calculatedPayment).toFixed(2); |
| 34 | ELtotal_interest.value = ((monthly*calculatedPayment)-principal).toFixed(2);//fix to 2 deciman places |
| 35 | //Show Results |
| 36 | document.getElementById('results').style.display='block'; |
| 37 | //Hide Loader |
| 38 | document.getElementById('loading').style.display='none'; |
| 39 | }else{ |
| 40 | console.log("Plase check your numbers"); |
| 41 | //Display an error |
| 42 | showError('Plase check your number'); |
| 43 | } |
| 44 | e.preventDefault(); |
| 45 | } |
| 46 | function showError(error){ |
| 47 | //Show Results |
| 48 | document.getElementById('results').style.display='none'; |