gpt4 book ai didi

JavaScript 贷款计算器

转载 作者:行者123 更新时间:2023-11-28 15:08:50 25 4
gpt4 key购买 nike

当我在 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>

最佳答案

it tells me that calculate is not defined

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

Uncaught SyntaxError: Unexpected number

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

monthly 1= Number.POSITIVE_INFINITY)&&

您错过了 Shift 键并输入了 1 而不是 !。该语法错误会导致函数声明无法成功求值,这就是当您尝试调用该函数时该函数不存在的原因。

关于JavaScript 贷款计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37585817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com