我有以下公式,正在尝试将其转换为 JavaScript:
=(x*(y/120000)*(1+(y/120000))^(z))/((1+(y/120000))^z-1)
我使用了以下内容,但我认为语法不正确,但我无法找出原因!
var price = ((parseInt(x.value)*(parseInt(y.value)/1200)*(Math.pow(1+parseInt(y.value)/12000)),parseInt(z.value))) / (Math.pow((1+(parseInt(y.value)/1200),(parseInt(z.value)-1)));
我的整个表单脚本如下:
<form name="priceCalc" action="">
<div align="center">
<select name="loanamount" onchange="price();">
<option value="130000">£130,000</option>
<option value="160000">£160,000</option>
</select>
<select name="interestrate" onchange="price();">
<option value="449">4.49%</option>
<option value="500">5%</option>
</select>
<select name="numberpayments" onchange="price();">
<option value="480">40yrs</option>
<option value="360">30yrs</option>
</select>
<br>
<div id="prices">Change items to see price</div>
</div>
</form>
<script>
function price() {
var x = document.priceCalc.loanamount;
var y = document.priceCalc.interestrate;
var z = document.priceCalc.numberpayments;
var price = ((parseInt(x.value)*(parseInt(y.value)/1200)*(Math.pow(1+parseInt(y.value)/12000)),parseInt(z.value))) / (Math.pow((1+(parseInt(y.value)/1200),(parseInt(z.value)-1)));
document.getElementById("prices").innerHTML = 'Price: ' + price;
}
</script>
任何帮助将不胜感激!
谢谢
最佳答案
=(x*(y/120000)*(1+(y/120000))^(z))/((1+(y/120000))^z-1)
翻译成类似
var x = parseInt(x.value,10),
y = parseInt(y.value,10)/120000,
z = parseInt(z.value,10)*12; // from your comments
var price = ( x*y*Math.pow(1+y,z) ) / (Math.pow(1+y,z)-1);
document.getElementById("prices").innerHTML ='Monthly repayment: £' + price.toFixed(2);
这是您的代码
window.onload = function() {
document.priceCalc.loanamount.onchange = price;
document.priceCalc.interestrate.onchange = price;
document.priceCalc.numberpayments.onchange = price;
}
function price() {
var x = parseInt(document.priceCalc.loanamount.value, 10);
var y = parseInt(document.priceCalc.interestrate.value, 10) / 12000;
var z = parseInt(document.priceCalc.numberpayments.value,10)*12;
var repayment = (x * y * Math.pow(1 + y, z)) / (Math.pow(1 + y, z) - 1);
console.log(x,y,z,x * y * Math.pow(1 + y, z),(Math.pow(1 + y, z) - 1),repayment)
document.getElementById("prices").innerHTML = 'Monthly repayment: £' + isFinite(repayment) && !isNaN(repayment) && repayment != Number.POSITIVE_INFINITY?repayment.toFixed(2):"0.00";
}
<form name="priceCalc" action="">
<div align="center">
<select name="loanamount">
<option value="0">Please select</option>
<option value="130000">£130,000</option>
<option value="160000">£160,000</option>
</select>
<select name="interestrate">
<option value="0">Please select</option>
<option value="449">4.49%</option>
<option value="500">5%</option>
</select>
<select name="numberpayments">
<option value="0">Please select</option>
<option value="480">40yrs</option>
<option value="360">30yrs</option>
</select>
<br>
<div id="prices">Change items to see price</div>
</div>
</form>
关于JavaScript 公式语法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32629731/