Skip to content
Advertisement

calculate compound profit in javascript

I have to calculate compound profit.

for example, I have $100, increasing 10% monthly, and I have to calculate the total profit for 12 months. And I need a profit of every month in an array.

I have tried this

let capital = 100;
let month = 12;
let profit_percentage = 10;
let total_profit;

for (i = 0; i <= month; i++) {
  total_profit = capital + (profit_percentage / 100) * 100;
  console.log(total_profit);
}

Advertisement

Answer

There seems to be a bit of missing info here, but if “profit” means the amount greater than the previous month:

Profit month over month:

const m = ((P, p) => 
  new Array(12).fill()
    .reduce((a, v) => (a.push(a.at(-1) * (1 + p)), a), [P])
    .map((v, i, a) => Math.round((a[i + 1] - v) * 100) / 100)
    .slice(0, -1)
)(100, .1);

Then, total profit:

Math.round((m.reduce((a, v) => ((a += v), a), 0) * 100)) / 100

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement