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>
ページ上部のスクリプトセクションを取得します。 – Oshadha
これを試してみましたが、うまくいきませんでした:L – James
@Hearner:CSS/JSの読み込みがHTMLレンダリングをブロックしているので、それはあまり良いアドバイスではありません。だからベストプラクティスは、JSを底に置くことです。こうすることで、JSがロードされる前にHTMLがレンダリングされます。 – Pimmol