2015-09-17 45 views
0

我有以下的公式,我試圖將其轉換爲JavaScript:沒有工作的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

有不匹配')''爲(Math.pow' – WhiteHat

+2

'x.value = parseInt函數(x.value)'等等將使你的JS公式更容易閱讀和避免多次調用'parseInt'。 – GillesC

+1

作爲一方不看代碼編輯器,一個好的編輯器會突出顯示你的'()'不匹配的事實爲你節省一些麻煩:) – GillesC

回答

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>

+0

這給了我的結果,但是他們想要得到答案應該是什麼?我不太清楚你在那裏用'x = parseInt(x.value,10)在做什麼,'10與10有什麼關係? –

+0

這是基數。如果輸入08或09,它可以解析爲八進制。請參閱更新的代碼 – mplungjan

+0

我看到改變z並沒有太大的作用。也許實際的計算是不正確的,但據我所見,我正確地轉換了你的計算 – mplungjan

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))))); 
+0

以下仍不起作用? 'var price =((parseInt(x.value)*(parseInt(y.value)/ 1200)*(Math.pow(1 + parseInt(y.value)/ 1200),parseInt(z.value)))/ (Math.pow((1+(parseInt函數(y.value)/ 1200),(parseInt函數(z.value)-1)))));」 –

+0

你得到的錯誤是什麼?嘗試新編輯的,我已經添加了1 + y的支架,這也應該解決數學問題 –

+0

抱歉,它仍然無法正常工作?如果有幫助,這是最初的Excel公式嗎? ** =(D2 *(C5/120000)*(1+(C5/120000))^(H2))/((1+(C5/120000))^ H2-1)**其中:D2 = x C5 = y H2 = z –

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)))); 
+0

仍不起作用?喃? –

+0

你有替換我發送給你的整行嗎?請如上所示替換整行,也請清除緩存並再次檢查。其工作 –

+0

仍然無法正常工作。如果您更改值z,它不會影響結果?這是最初的Excel公式** =(D2 *(C5/120000)*(1+(C5/120000))^(H2))/((1+(C5/120000))H2-1)** D2 = x C5 = y H2 = z –

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>

+0

這可能是我的代碼和正確的修正。查看我的更新 – mplungjan

+0

是的,對不起。我被數學弄糊塗了一段時間,然後它一定是你的代碼,我最終修改了! –

+0

我在改變數值的同時添加了對無窮大和NAN的測試。 – mplungjan