2011-12-17 20 views
2

我在閱讀Javascript - 權威指南。我通常會嘗試模仿他們給出的例子,這樣我就可以更快地學習它,這就是我學習的方式。但我重寫了這個Javascript,它似乎沒有工作。每當我輸入數字並命中計算它不計算。「Javascript - 權威指南」不起作用的示例之一

我已經試過在其他地方尋找答案,但什麼都沒發現。

<html> 
<body> 
<head><title>Factorials</title></head> 



<form name="loandata"> 
    <table> 
     <tr> 
      <td colspan="3"><b>Enter Loan Information:</b></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> 
      <td>2)</td> 
      <td>Annual percentage rate of interest:</td> 
      <td><input type="text" name="interest" size="12" 
         onchange="calculate();" /></td> 
     </tr> 
     <tr> 
      <td>3)</td> 
      <td>Repayment period in years:</td> 
      <td><input type="text" name="years" size="12" 
         onchange="calculate();" /></td> 
     </tr> 
     <tr> 
      <td colspan="3"> 
       <input type="button" value="Compute" onclick="calculate();" /> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="3"> 
       <b>Payment Information:</b> 
      </td> 
     </tr> 
     <tr> 
      <td>4)</td> 
      <td>Your monthly payment will be:</td> 
      <td><input type="text" name="payment" size="12" /></td> 
     </tr> 
     <tr> 
      <td>5)</td> 
      <td>Your total payment will be:</td> 
      <td><input type="text" name="total" size="12" /></td> 
     </tr> 
     <tr> 
      <td>6)</td> 
      <td>Your total interest payments will be:</td> 
      <td><input type="text" name="totalinterest" size="12" /></td> 
     </tr> 
    </table> 
</form> 

<script> 
function calculate() { 
    // Get the user's input from the form. Assume it is all valid. 
    // Convert interest from a percentage to a decimal, and convert form 
    // an annual rate to a monthly rate. Convert payment period in years 
    // to the number of monthly payments. 
    var principal = document.loandata.principal.value; 
    var interest = document.loandata.interest.value/100/12; 
    var payments = document.loandata.years.value * 12; 

    // Now compute the monthly payment figure, using esoteric math.. 
    var x = Math.pow(1 + interest, payments); 
    var monthly = (principal*x*interest)/(x-1); 

    // Check that the result is a finite number. If so, display the results. 
    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); 
    } 
    // Otherwise, the user's input was probably invalid, so don't display anything. 
    else { 
     document.loandata.payment.value = ""; 
     document.loandata.total.value = ""; 
     document.loandata.totalinterest.value = ""; 
    } 
} 

// This simple method rounds a number to two decimal places. 
function round(x) { 
    return Math.round(x*100)/100; 
} 
</script> 

</body> 
</html> 
+0

你觀察到了什麼行爲? – duffymo 2011-12-17 23:58:08

+0

究竟是什麼問題? 「似乎沒有工作」有點含糊。 – graphicdivine 2011-12-17 23:58:49

+2

您至少有[一個語法錯誤](http://jsfiddle.net/V5U6e/)。請確保始終使用Firebug或Chrome控制檯進行調試。 (編輯:注意我不得不改變jsfiddle中的一些設置,這個工程:http://jsfiddle.net/V5U6e/2/) – 2011-12-17 23:58:57

回答

4

我注意到的第一件事是,你缺少一個支架:

if (!isNaN(monthly) && (monthly != Number.POSITIVE_INFINITY) && (monthly != Number.NEGATIVE_INFINITY) *)* {

我發現JSLint幫助我與這些類型的語法錯誤很多。如果您使用IDE,則可以使用一些集成工具來獲取JSLint以使用它們。

Jared還間接地談論了另一個網站JSFiddle,它可以用來調試HTML + CSS + JS。

+0

這實際上允許代碼至少運行:http://jsfiddle.net/V5U6e/2/ – 2011-12-18 00:01:56

+0

Thanx,AmazingHorse! – Opposes 2011-12-18 00:11:59