Loan Calculator

[Total: 0    Average: 0/5]

This file shows how to create a form and then perform simple calculations based upon that input and other user choices(clicks).

Body Code:

<form name="loandata">
<table>
<tr><td colspan=3><div class=result>enter loan information:</div></td></tr>
<tr><td><div class=result>1)</div></td><td><div class=result>aount of loan:</div></td><td><input type=text name=principal size=12 onChange="calculate()"></td></tr>
<tr><td><div class=result>2)</div></td><td><div class=result>rate of interest(anual):</div></td><td><input type=text name=interest size=12 onChange="calculate()"></td></tr>
<tr><td><div class=result>3)</div></td><td><div class=result>length in years of loan:</div></td><td><input type=text name=years size=12 onChange="calculate()"></td></tr>
<tr><td colspan=3><div class=result><input type=button value="compute" onClick="calculate()">Payment Information:</div></td></tr>
<tr><td><div class=result>4)</div></td><td><div class=result>Monthly Payment:</div></td><td><input type=text name=payment size=12></td></tr>
<tr><td><div class=result>5)</div></td><td><div class=result>total cost:</div></td><td><input type=text name=total size=12></td></tr>
<tr><td><div class=result>6)</div></td><td><div class=result>total interest:</div></td><td><input type=text name=totalinterest size=12></td></tr>
</table>
</form>
<script type=text/javascript>
function calculate() {
  var principal = document.loandata.principal.value;
  var interest  = document.loandata.interest.value / 100 / 12;
  var payments  = document.loandata.years.value * 12;
  var x         = Math.pow(1 + interest, payments);
  var monthly   = (principal*x*interest)/(x-1);
  if (!isNaN(monthly) &&
      (monthly != Number.POSITIVE_INFINITY) &&
	  (monthly != Number.NEAGTIVE_INFINITY)) {
    document.loandata.payment.value=Math.round(monthly);
	document.loandata.total.value=Math.round(monthly*payments);
	document.loandata.totalinterest.value=Math.round((monthly*payments) - principal);
  }
  else {
    document.loandata.payment.value="";
	document.loandata.total.value="";
	document.loandata.totalinterest.value="";
  }
}
function round(x) {return Math.round(x *100)/100;}
</script>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *