Future value calculation with an interesting mix with JavaScript

I am trying to work on a scenario where a user enters a monthly income and receives future value with a compound interest in 30 years. As of now, I have assigned some values ​​for testing.

// Future Value
var investment = 800;
var annualRate = 2;
var monthlyRate = annualRate / 12 / 100;
var years = 30;
var months = years * 12;
var futureValue = 0;

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

The problem is that I actually build this from an Excel spreadsheet that uses the built-in FV () formula and when cross-checking, my results are completely disabled ... Any idea what I'm doing wrong, since I'm not in financial math at all . Thanks in advance.

+4
source share
4 answers

Math.pow , futureValue . 1 + monthlyRate. :

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

:

futureValue = investment * (Math.pow(1 + monthlyRate, months) - 1) / monthlyRate;
+8
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

0

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;
    }
0
source

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);
}
0
source

Source: https://habr.com/ru/post/1785888/


All Articles