2016-06-02 19 views
0

Chromeで次のコードを実行すると、calculateが定義されていないことがわかります。コードはローン計算機であることを意図しています。Javascript Loan Calculator

<!--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> 
+0

ページ上部のスクリプトセクションを取得します。 – Oshadha

+0

これを試してみましたが、うまくいきませんでした:L – James

+1

@Hearner:CSS/JSの読み込みがHTMLレンダリングをブロックしているので、それはあまり良いアドバイスではありません。だからベストプラクティスは、JSを底に置くことです。こうすることで、JSがロードされる前にHTMLがレンダリングされます。 – Pimmol

答えて

4

それは計算している私に語っ

が定義されていないことはありませんが、その前に、それはあなたに伝えます:

Uncaught SyntaxError: Unexpected number 

最初の最初のエラーに対処します。エラーが発生すると、さらにエラーが発生します。

monthly 1= Number.POSITIVE_INFINITY)&& 

あなたはシフトキーを逃し、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のようなリンターをピックアップする必要があります。それはあなたの構文をチェックしておくのに役立ちます。

関連する問題