By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Compound Interest – JavaScript task

+2 votes

Write a JS function that calculates the total accumulated value for a monetary deposit by given principal sum, interest rate, compounding frequency and overall length. At the beginning of the task you are given the compounding period, which is inversely related to the frequency. You need to express the frequency as how many times in a year the interest is compounded. For instance, a 3-month period means the interest will be updated 4 times in a year. Any percentages need to be expressed as a fraction.

The input comes as an array of numbers:

  • The first value is the principal sum
  • The second is the interest rate in percent
  • The third is the compounding period in months
  • The final value is the total time span, given in years.


[1500, 4.3, 3, 6]


[100000, 5, 12, 25]


The output should be printed to the console, with two decimal places.

asked in JavaScript category by user andrew

1 Answer

+1 vote

Here is my javascript solution:

function compoundInterest([principal, interest, period, time]) {
    [principal, interest, period, time]=[principal, interest, period, time].map(Number);

    interest /= 100;//or interest = interest/100;
    let frequency = 12 / period;

    let total = principal * Math.pow(1 + interest / frequency, frequency * time);



compoundInterest([100000, 5, 12, 25]);


answered by user eiorgert