How to calculate daily, weekly, monthly compound interest in Javascript?

I need a very accurate calculator formula. Right now my formula works for monthly installments with a monthly recipe, but when you use the weekly installments with a monthly recipe, I don’t know how to set up the formula. You can see my calculator in action by downloading and unzipping attached files.

It is still the most accurate calculator I have found on the Internet. I would be happy if I could get a Javascript formula to match the values ​​generated by this calculator.

The problem with the weekly installments and the monthly recipe is that although there are 52 weeks in a year, some months have 5 weeks and others 4. I don’t think their calculator takes this into account; I think that they simply base their calculations on 52 weeks and every 4 weeks, they are of interest.

Here is the formula I'm using:

var P = startingAmount;
var Y = yearsOfInvesting;
var c = getAdditionalContributionsPerPeriod;
var t = getTermsPerPeriod;
var n = t * Y;
var r = interestRate / 100 / t; // interestRate [%]
var z = 1 + r;
total = P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z) / r;

Given the scenario

  • $ 1000
  • 50 dollars a week
  • 10 years,
  • monthly recipe

another calculator says it totalshould be $ 30.007, rounding decimals up. The closest I came to this uses this formula: weekly installments and weekly recipes (but I want a monthly recipe!):

var P = 1000;//startingAmount;
var Y = 0;//yearsOfInvesting;
var c = 50;//
var n = 520;//t * Y;
var r = .02/52;
var z = 1 + r;

mz = P * Math.pow(z, n) + c * (Math.pow(z, n + 1) - z) / r;
document.write(mz);

How can I use my formula for weekly and daily installments?

+3
source share
1

... , , , , ( , , , ):

function calc( startingAmount, yearsOfInvesting, additionalContributionsPerPeriod, interestRate ) {
    var interestPerDay = ( interestRate / 365 );
    var total = startingAmount;
    var date = new Date( new Date().getFullYear() + new Date().getMonth() + 1, 1 );
    var endDate = new Date( date.getFullYear() + yearsOfInvesting, date.getMonth(), date.getDate() - 1 );
    var startingWeekday = date.getDay();
    var startingDate = date.getDate();
    var runningInterest = 0;
    while( Date.parse( date.toString() ) < Date.parse( endDate.toString() ) ) {
        date.setDate( date.getDate() + 1 );
        runningInterest = runningInterest + total * interestPerDay;
        if( date.getDay() == startingWeekday ) {
            total = total + additionalContributionsPerPeriod;
        }
        if( date.getDate() == startingDate ) {
            total = total + runningInterest;
            runningInterest = 0;
        }
    }
    total = total + runningInterest;
    return total;
}
function calc2( startingAmount, yearsOfInvesting, additionalContributionsPerPeriod, interestRate ) {
    var interestPerDay = ( interestRate / 365 );
    var total = startingAmount;
    var runningInterest = 0;
    for( var day = 1; day <= 365 * yearsOfInvesting; day++ ) {
        runningInterest = runningInterest + total * interestPerDay;
        if( day % 7 == 0 ) {
            total = total + additionalContributionsPerPeriod;
        }
        if( day % 30 == 0 ) {
            total = total + runningInterest;
            runningInterest = 0;
        }
    }
    total = total + runningInterest;
    return total;
}
document.write( 3647 + "<br>" + calc( 1000, 1, 50, 0.02 ) + "<br>" + calc2( 1000, 1, 50, 0.02 ) );
document.write( "<br><br>" );
document.write( 6347 + "<br>" + calc( 1000, 2, 50, 0.02 ) + "<br>" + calc2( 1000, 2, 50, 0.02 ) );
document.write( "<br><br>" );
document.write( 14779 + "<br>" + calc( 1000, 5, 50, 0.02 ) + "<br>" + calc2( 1000, 5, 50, 0.02 ) );
document.write( "<br><br>" );
document.write( 30007 + "<br>" + calc( 1000, 10, 50, 0.02 ) + "<br>" + calc2( 1000, 10, 50, 0.02 ) );
document.write( "<br><br>" );
document.write( 31673 + "<br>" + calc( 1000, 10, 50, 0.03 ) + "<br>" + calc2( 1000, 10, 50, 0.03 ) );
document.write( "<br><br>" );
document.write( 33460 + "<br>" + calc( 1000, 10, 50, 0.04 ) + "<br>" + calc2( 1000, 10, 50, 0.04 ) );
document.write( "<br><br>" );
document.write( 35378 + "<br>" + calc( 1000, 10, 50, 0.05 ) + "<br>" + calc2( 1000, 10, 50, 0.05 ) );
document.write( "<br><br>" );
document.write( 772849953 + "<br>" + calc( 1000, 55, 50, 0.20 ) + "<br>" + calc2( 1000, 55, 50, 0.20 ) );
+1

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


All Articles