Problems › JavaScript › finance
Balance after yearly compounding
An account reinvests its interest once a year. Report the balance after a whole number of years.
- Each year the balance gains floor(balance × rate / 100) before the year ends.
- Zero years returns the untouched principal.
- Interest is credited in whole minor units, rounding down each year.
compoundBalance(principal: int, ratePercent: int, years: int) → int
Where you start
function compoundBalance(principal, ratePercent, years) {
}
Worked examples
| Call | Result |
|---|---|
compoundBalance(1000, 10, 2) | 1210 |
compoundBalance(100, 5, 3) | 115 |
compoundBalance(5000, 0, 5) | 5000 |
compoundBalance(0, 10, 5) | 0 |
Hint
Loop the years, adding the floored interest each pass.
Reference solution in JavaScript
function compoundBalance(principal, ratePercent, years) {
let balance = principal;
for (let i = 0; i < years; i++) balance += Math.floor((balance * ratePercent) / 100);
return balance;
}