title: Converting future value calculation of compound interest calculator to javascript
body: <p>I am currently trying to convert an equation in google sheets to Javascript but I am getting different results. </p>
  
  <p>I am trying to replicate the following formula from <a href="https://www.vertex42.com/Calculators/compound-interest-calculator.html#rate-per-period" rel="nofollow noreferrer">here</a>:<br><br>
  <strong>Original equation:</strong></p>
  
  <pre><code>F = P*(1+rate)^nper + A*( ((1+rate)^nper - 1)/rate )
  </code></pre>
  
  <p><strong>This is what I currently have: (In my actual code snippet below names of variables are different)</strong></p>
  
  <pre><code>v = ((p*(Math.pow((1+rate),nper))) + (A*(Math.pow((1+rate),nper - 1))/rate));
  
  </code></pre>
  
  <p><br>
  <strong>Extra Information from original site:</strong><br>
  r = nominal annual interest rate (decimal)<br>
  n = number of compounding periods per year<br>
  p = number of payment periods per year<br>
  rate = rate per payment period<br>
  nper = total number of payment periods<br>
  A = an amount added to the principal at the end of each payment period<br></p>
  
  <p>rate = ((1+r/n)^(n/p))-1<br>
  nper = p * t<br>
  Total Payments = A*nper<br>
  Total Interest = F - P - Total Payments<br></p>
  
  <p><br>
  <strong>Full context (My Vue Code):</strong></p>
  
  <pre><code>
          var app = new Vue({
            el: '#app',
            data: {
              principal: 0,
              interest_rate: 0,
              time_invested: 0,
              additional_investment: 0,
              additional_investment_frequency : 1,
              options: {
                  additional_investment_frequency: {
                  "1": "Yearly",
                  "12": "Monthly",
                  "26": "Fortnightly",
                  "52": "Weekly",
                  "365": "Daily"
                  }
              },
              compound_frequency : 1,
              options: {
                  compound_frequency: {
                  "1": "Yearly",
                  "12": "Monthly"
                  }
              }
            },
            methods:{
          },
            computed: {
              total_pay_periods: function () {
                  // calculate total number of pay periods
                  total_pay_periods = parseFloat(this.additional_investment_frequency) * parseFloat(this.time_invested);
                  return parseFloat(this.additional_investment_frequency) * parseFloat(this.time_invested);
              },
              total_additional_investment: function () {
                  // calculate total number of pay periods
                  total_pay_periods = parseFloat(this.additional_investment_frequency) * parseFloat(this.time_invested);
                  // times total pay periods by additional investment installment amount to get overall total additional investment
                  return parseFloat(this.additional_investment) *  total_pay_periods;
              },
              rate: function () {
                  ir = parseFloat(this.interest_rate) / 100;
                  cf = parseFloat(this.compound_frequency);
                  aif = parseFloat(this.additional_investment_frequency);
                  return (Math.pow((1+ir/cf),(cf/aif))-1);
                  // return (((1+ir/cf)^(cf/aif))-1);
              },
              future_value: function () {
                  p = parseFloat(this.principal);
                  r = parseFloat(this.rate);
                  tpp = parseFloat(this.total_pay_periods);
                  ai = parseFloat(this.additional_investment);
  
                  // F = P*(1+rate)^nper + A*( ((1+rate)^nper - 1)/rate )
                  // https://www.vertex42.com/Calculators/compound-interest-calculator.html#rate-per-period
  
                  // calculate final future value
                  v = ((p*(Math.pow((1+r),tpp))) + (ai*(Math.pow((1+r),tpp - 1))/r));
                  // convert to 2 decimal places
                  return  v.toFixed(2);
              }
            }
          })
  </code></pre>