Here is the formula for compound interest in JavaScript:
index.tsx548 chars16 lines
In this example, we have a function called compoundInterest
which takes four arguments:
principal
- the initial amount of the investmentrate
- the annual interest rate expressed as a decimaltime
- the duration of the investment in yearscompoundsPerYear
- the number of times the interest is compounded per yearThe 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