title: How do I calculate daily, weekly, monthly compound interest accurately in Javascript?
body: <p>I need a very accurate calculator formula.  Right now my formula works for monthly contributions with monthly compounding, but when you use weekly contributions with monthly compounding, I don't know how to adjust the formula. You can see my calculator in action by downloading and unzipping the attached files.</p>
  
  <p>So far, <a href="http://dinkytown.com/java/CompoundSavings.html" rel="nofollow noreferrer">this is the most accurate calculator</a> I've found on the web.  I'd be happy if I could get a Javascript formula to match the values generated by this calculator.</p>
  
  <p>The problem with weekly contributions and monthly compounding is that while there are 52 weeks in a year, some months have 5 weeks and others have 4.  I don't think their calculator takes this into account; I think they simply base their calculation on 52 weeks and every 4 weeks they compound the interest.</p>
  
  <p>Here is the formula I'm using:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>Given a scenario of</p>
  
  <ul>
  <li>$1000 principal,</li>
  <li>$50 per week contribution,</li>
  <li>10 years,</li>
  <li>monthly compounding,</li>
  </ul>
  
  <p>the other calculator says <code>total</code> should be $30,007, rounding the decimals up.  The closest I've come to this is using this formula is by weekly contributions and weekly compounding (But I want monthly compounding!):</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>How can I make my formula work for weekly and daily contributions?</p>