我怎么用Javascript写这个公式呢?

How would I write this formula in Javascript?

本文关键字:Javascript      更新时间:2023-09-26

我有一个项目,我正在工作,它需要计算抵押贷款的计算,但我有麻烦把公式放入javascript。

公式为:

M = pi (1 + I)^n/(1 + I)^n - 1

任何帮助都是感激的,谢谢

p =贷款本金

I =利息

N = Term

将其分解为一系列步骤。

  • 乘法非常简单:I*(1+I)
  • 分区相同:I/(1+I)
  • n的幂表示为:Math.pow(3, 5); //3 to the power of 5

Math.pow()可能是你唯一不知道的。


不相关但有用,

将公式包装成函数,就有了一个抵押贷款计算函数

calculateMortgage(p,i,n) { result = //translate the formula in the way I indicated above return result; }

并这样命名:

var mortgage = calculateMortgage(300,3,2); // 'mortgage' variable will now hold the mortgage for L=300, I=3, N=2

另外,你发布的公式真的没有任何意义-为什么在P &一开始是I吗?缺失的东西。

试试:Math.pow(p*i*(1+i),n)/Math.pow(1+i,n-1)

Math.pow(a,2)等于a^2

如果p不带分子,则这个

p * (Math.pow(i*(1+i),n)/Math.pow(1+i,n-1))

p * (Math.pow((i+i*i),n)/Math.pow(1+i,n-1))

var M;
var P;
var I;
M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);

你觉得这样合适吗?我从这里得到了正确样式的公式。

就像上面Nicholas说的,你可以使用函数使它更容易。

var M;
function calculateMortgage(P, I, N){
    M = P*(Math.pow(I*(1+I),n)) / (Math.pow((1+I),n)-1);
    alert("Your mortgage is" + M);
}

只要调用calculateMortgage(100, 100, 100);和你的值,它就会自动给你答案。