title: Compounding interest monthly with a deposit
body: <p>I want to compound interest on a weekly/fortnightly/monthly/annual basis.</p>
  <p>I also want an option to have a deposit amount that can be added in.</p>
  <p>I have already tried the standard formula of calculating the final amount accrued, as seen here:</p>
  <p><a href="https://i.stack.imgur.com/RK9tM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RK9tM.png" alt="formula" /></a><br />
  <sub>(source: <a href="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQxXp8OrXqBGn161piBwu-GExFUjfswrSWEvrAzPcoEQapQ98eM" rel="nofollow noreferrer">gstatic.com</a>)</sub></p>
  <p>For example here is my method for calculating the interest compounding weekly:</p>
  <pre><code>function calculateWeekly(state: any) {
    const { savings, deposit ,interest, timePeriodSelector, timePeriodLength } = state;
  
    let numberOfYears = 0;
  
    if (timePeriodSelector === &quot;weekly&quot;) {
      numberOfYears = timePeriodLength / weeksInAYear;
    } else if (timePeriodSelector === &quot;fortnightly&quot;) {
      numberOfYears = (timePeriodLength / weeksInAYear) * 2;
    } else if (timePeriodSelector === &quot;monthly&quot;) {
      numberOfYears = (timePeriodLength / weeksInAYear) * weeksInAMonth;
    } else if (timePeriodSelector === &quot;annually&quot;) {
      numberOfYears = (timePeriodLength / weeksInAYear) * weeksInAYear;
    }
  
    const weeklyRate = interest / 100 / weeksInAYear;
    const lengthOfCompunding = numberOfYears * weeksInAYear;
  
    let startingFigure = parseInt(savings) + parseInt(deposit);
  
    //total gets added on for every time cycle of week
    let total =
      (startingFigure * (Math.pow(1 + weeklyRate, lengthOfCompunding) - 1)) / weeklyRate;
  
    return roundToTwoDP(total);
  }
  
  </code></pre>
  <p>The issue with the above code is that the deposit gets added into the calculation every time the interest accrues. So a deposit of $10 weekly for 10 weeks will actually get added up to $100.</p>
  <p>I attempted a method to accrue the interest using a loop for each week here:</p>
  <pre><code>  // loops how many times to compound the interest
    for(let i = numberOfYears - (1/weeksInAYear); i &gt; 0; i-= (1/weeksInAYear)){
      let interestGained = (total * (Math.pow((1 + weeklyRate), lengthOfCompunding))) - total;
      total += interestGained + savings;
    }
  </code></pre>
  <p>Thanks for any help!</p>