0

Write a function called "computeCompoundInterest".

Given a principal, an interest rate, a compounding frequency, and a time (in years), "computeCompoundInterest" returns the amount of compound interest generated.

var output = computeCompoundInterest(1500, .043, 4, 6);
console.log(output); // --> 438.8368221341061

Reference: https://en.wikipedia.org/wiki/Compound_interest#Calculation_of_compound_interest This shows the formula used to calculate the total compound interest generated.

Problem is i am trying to use this formula and can't get it right

function computeCompoundInterest(p, i, compoundingFrequency, timeInYears) {

  p = p * (1 + (i/4)))^(compoundingFrequency*timeInYears)

}

I tried to step through each calculation and it looks like once I get to:

p = 1500 * (1 + 0.043/4)^ 4 x 6 //compoundingFrequency = 4 and timeInYears = 6

I am doing something wrong. This website seems to get a decimal number when you (1 + (i/4)))^(compoundingFrequency*timeInYears)

Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
  • 2
    Have you tried Math.pow(x, y)? – Knerd May 17 '17 at 14:35
  • 1
    ^ is bitwise XOR not power. – Alex K. May 17 '17 at 14:35
  • 2
    `^` is [bitwise XOR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_XOR)) - not `pow`. You probably want `p = 1500 * Math.pow((1 + 0.043/4), 4) * 6` – Matt May 17 '17 at 14:36

1 Answers1

4

^ operator is XOR operator.

For exponentation you should use function Math.pow (like Math.pow(2,3) === 8) or ** operator (like 2**3 === 8)

Ginden
  • 5,149
  • 34
  • 68