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
JavaScript
x
10
10
1
let capital = 100;
2
let month = 12;
3
let profit_percentage = 10;
4
let total_profit;
5
6
for (i = 0; i <= month; i++) {
7
total_profit = capital + (profit_percentage / 100) * 100;
8
console.log(total_profit);
9
}
10
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:
JavaScript
1
8
1
const m = ((P, p) =>
2
new Array(12).fill()
3
.reduce((a, v) => (a.push(a.at(-1) * (1 + p)), a), [P])
4
.map((v, i, a) => Math.round((a[i + 1] - v) * 100) / 100)
5
.slice(0, -1)
6
)(100, .1);
7
8
Then, total profit:
JavaScript
1
3
1
Math.round((m.reduce((a, v) => ((a += v), a), 0) * 100)) / 100
2
3