[英]Javascript Loan Calculator
当我在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>
它告诉我计算未定义
它可以,但是在此之前它告诉您:
Uncaught SyntaxError: Unexpected number
首先处理第一个错误。 错误会导致进一步的错误。
monthly 1= Number.POSITIVE_INFINITY)&&
您错过了Shift键并输入1
而不是!
。 语法错误使函数声明无法成功求值,这就是为什么当您尝试调用函数时该函数不存在的原因。
您有许多语法错误,这是您唯一的问题。 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这样的短绒。 它将帮助您检查语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.