1 // LoanPayment JavaScript source
   2 
   3 function calculate() {
   4     var principal = document.loandata.principal.value;
   5     var interest = document.loandata.interest.value / 100 / 12;
   6     var payments = document.loandata.years.value * 12;
   7 
   8     var x = Math.pow(1 + interest, payments);
   9     var monthly = (principal * x * interest) / (x-1);
  10 
  11     if(!isNaN(monthly) &&
  12             (monthly != Number.POSITIVE_INFINITY) &&
  13             (monthly != Number.NEGATIVE_INFINITY)) {
  14         document.loandata.payment.value = round(monthly);
  15         document.loandata.total.value = round(monthly * payments);
  16         document.loandata.totalinterest.value = round((monthly * payments) - principal);
  17     } else {
  18         document.loandata.payment.value = "";
  19         document.loandata.total.value = "";
  20         document.loandata.totalinterest.value = "";
  21     }
  22 }
  23 
  24 function round(x) {
  25     return Math.round(x*100)/100;
  26 }
  27 
  28     
web biohackers.net