Calculating future value with compound interest with JavaScript

梦想的初衷 提交于 2019-12-03 03:52:49

The Math.pow is unnecessary, since you are calculating and incrementing futureValue month by month. Simply multiply by 1 + monthlyRate. You also want to add the current value of the investment to the new investment before multiplying:

for ( i = 1; i <= months; i++ ) {
   futureValue = (futureValue + investment) * (1 + monthlyRate);
}

Alternatively, you can also calculate this in one go with the following formula:

futureValue = investment * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate;
function FVcalc(PresentAmount,InterestRate,NumberOfYears) {
    var timescompound = 1;
    var AnnualInterestRate = (InterestRate/100)/timescompound;
    var Years= NumberOfYears

    var Periods=timescompound*Years;
    var NumPayments=Periods;
    var Prin=PresentAmount;

    MonthPayment=Math.floor((Prin)*(Math.pow((1+AnnualInterestRate),(Periods)))*100)/100;
    FVFactor=(Math.pow((1+AnnualInterestRate),(Periods)))
    return MonthPayment
}

http://www.uic.edu/classes/actg/actg500/pfvatutor.htm

This is my way of writing code for compound interest

function call()
    {
        var A = Principle;
        var B = Interest;
        var C = Years;
        var D = 0;
        var E = A*B/100;
        D+=E;
        var f=E+A;
        document.write("0 year: Interest "+E+" Principal: "+f);
        document.write("<br />");
        for (var i=1; i<C; i++)
        {
            E=f*B/100;
            D+=E;
            f=E+f;
            document.write(i+"year: Interest "+E+" Principal:"+f);
            document.write("<br />");
        }

    return false;
    }

Also I found with this alternative:

function compoundInterest(principal, annual_rate, n_times, t_years) {
    return principal*(Math.pow(1 + annual_rate/n_times, n_times*t_years) - 1);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!