我有以下公式,并尝试将其转换为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>
非常感谢任何帮助!
由于
答案 0 :(得分:2)
=(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>
答案 1 :(得分:1)
var price = ((parseInt(x.value)*(parseInt(y.value)/120000)*(Math.pow(1+parseInt(y.value)/120000),parseInt(z.value))) / (Math.pow((1+(parseInt(y.value)/120000),(parseInt(z.value)-1)))));
此外,在您的第一行中,您有/120000
,但在您的计算中,您有/1200
另据gillesc的建议:
var x = parseInt(x.value);
var y = parseInt(y.value);
var z = parseInt(z.value);
var price = ((x*(y/120000)*(Math.pow((1+y)/120000),z)) / (Math.pow((1+(y/120000),(z-1)))));
答案 2 :(得分:0)
请尝试以下更新的公式: -
var price = ((parseInt(x.value) * (parseInt(y.value) / 1200) * (Math.pow(((parseInt(1) + parseInt(y.value) / 12000)), parseInt(z.value)))) / (Math.pow((parseInt(1) + (parseInt(y.value) / 1200)), (parseInt(z.value) - 1))));
答案 3 :(得分:0)
我最终找到了一条路:
window.onload = function() {
document.repaymentcalc.loanamount.onchange = repayment;
document.repaymentcalc.interestrate.onchange = repayment;
document.repaymentcalc.numberpayments.onchange = repayment;
}
function repayment() {
var x = parseInt(document.repaymentcalc.loanamount.value, 10);
var y = parseInt(document.repaymentcalc.interestrate.value, 10) / 120000;
var z = parseInt(document.repaymentcalc.numberpayments.value, 10) * 12;
var repayment = y * x * Math.pow((1 + y), z) / (Math.pow((1 + y), z) - 1);
document.getElementById("repayments").innerHTML = 'Monthly repayment: £' + repayment.toFixed(2);
}
<form name="repaymentcalc" action="">
<div align="center">
</br>
Loan Amount
<select name="loanamount">
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
</select>
</p>
Interest Rate
<select name="interestrate">
<option value="100">1.00%</option>
<option value="200">2.00%</option>
<option value="300">3.00%</option>
</select>
</p>
Term
<select name="numberpayments">
<option value="5">5yrs</option>
<option value="10">10yrs</option>
</select>
</p>
<br>
<div id="repayments">Monthly repayment: £837.85</div>
</div>
</form>