Javascript贷款计算器

时间:2016-06-02 07:54:38

标签: javascript

当我在Chrome中运行以下代码时,它告诉我calculate未定义。该代码旨在成为贷款计算器。

   <!--DOCTYPE html--> 
     <form name="loandata">
    <table>
        <tr><td colspan=3><h1><b>Enter Loan Information</b></h1></td></tr>
        <tr>
            <td>1)</td>
            <td>Amount of the loan (any currency):</td>
            <td><input type=text name=principal size=12 onChange="calculate()"></td>
        </tr>
    </tr>
        <tr>
            <td>2)</td>
            <td>Annual percentage rate of interest</td>
            <td><input type=text name=interest size=12 onChange="calculate()"></td>
        </tr>
    </tr>
        <tr>
            <td>3)</td>
            <td>Repayment period in years:</td>
            <td><input type=text name=years size=12 onChange="calculate()"></td>
        </tr>
    </tr>
    <tr><td colspan=3>
        <h1><b>
            <input type=button value="Compute" onClick="calculate()">
            Payment Information
        </h1></b> 
    </td></tr>
    <tr>
        <td>4)</td>
        <td>Your monthly payment:</td>
        <td><input type=text name=total interest size=12></td>
    </tr>
    </table>
    </form>
    <script type="text/javascript">
    function calculate(){
        var principal= document.loandata.principal.value; //Get the input principal amount
        var interest = document.loandata.interest.value/100/12; //Get the input interest amnount 
        var payments= document.loandata.years.value*12; //get the number of years to payback the loan
        var y =math.pow(1+ interest, payments);
        var monthly = (principal*y*interest)/(y-1);
        if(!isNaN(monthly) &&
            (monthly 1= Number.POSITIVE_INFINITY)&&
            (monthly != Number.NEGATIVE_INFINITY){
                document.loandata.payment.value =round(monthly);
                    document.loandata.total.value= round(monthly*payments);
                    document.loandata.totalinterest.value=
                    round(*(monthly*payments)-principal);
                }
            }
            function round(y){
                return Math.round(y*100)/100;
            }
    </script>

2 个答案:

答案 0 :(得分:4)

  

它告诉我没有定义计算

确实如此,但在此之前它告诉你:

Uncaught SyntaxError: Unexpected number

首先处理第一个错误。错误会导致进一步的错误。

monthly 1= Number.POSITIVE_INFINITY)&&

您错过了班次键并输入1而不是!。该语法错误会阻止函数声明被成功评估,这就是当您尝试调用函数时该函数不存在的原因。

答案 1 :(得分:0)

您有许多语法错误,这是您唯一的问题。 math.pow应该是Math.pow,1 =应该是!=,并且你有一些问题,括号不匹配。

function calculate(){
  var principal = document.loandata.principal.value; //Get the input principal amount
  var interest = document.loandata.interest.value/100/12; //Get the input interest amnount 
  var payments = document.loandata.years.value*12; //get the number of years to payback the loan
  var y = Math.pow(1+ interest, payments);
  var monthly = (principal*y*interest)/(y-1);
  if(!isNaN(monthly) && monthly !== Number.POSITIVE_INFINITY && monthly !== Number.NEGATIVE_INFINITY ){
    document.loandata.payment.value = round(monthly);
    document.loandata.total.value = round(monthly*payments);
    document.loandata.totalinterest.value = round((monthly * payments) - principal);
  }
}

你应该像jshint一样拾取一个linter。它有助于控制语法。