formula del interes compuesto in javascript

Here is the formula for compound interest in JavaScript:

index.tsx
function compoundInterest(principal, rate, time, compoundsPerYear) {
  var amount = principal * Math.pow((1 + rate / compoundsPerYear), compoundsPerYear * time);
  return amount - principal;
}

// Example Usage
var principal = 10000; // Principal Amount
var rate = 0.08; // Annual Interest Rate
var time = 2; // Time in Years
var compoundsPerYear = 12; // Number of times interest is compounded per year

var interest = compoundInterest(principal, rate, time, compoundsPerYear);

// Output the result
console.log("Compound Interest: " + interest);
548 chars
16 lines

In this example, we have a function called compoundInterest which takes four arguments:

  1. principal - the initial amount of the investment
  2. rate - the annual interest rate expressed as a decimal
  3. time - the duration of the investment in years
  4. compoundsPerYear - the number of times the interest is compounded per year

The function then calculates and returns the amount of compound interest earned during the investment period.

We have provided an example usage of the function, where we pass in some example values for principal, rate, time, and compoundsPerYear. The function then calculates the compound interest and stores it in the variable interest.

Finally, we output the result to the console using console.log().

gistlibby LogSnag