Please, help needed! I'm trying to make a small js compounding calculator
function calculateCompound(amount, earning, remains, roi, compound) {
if (remains > 0) {
var ini_earning = earning;
var accrual = amount * roi;
earning = accrual * (1 - compound);
var new_amount = amount + accrual - earning;
var new_earning = ini_earning + earning;
remains--;
calculateCompound(new_amount, new_earning, remains, roi, compound);
}
return earning + amount;
}
calculateCompound(100, 0, 5, .1, .5);
but after 5 calls it returns the initial values (105 in this case)
Many thanks in advance!
returnbefore the internalcalculateCompound(...)?earning + amount. Now look before the return call, never mind the recursive call... is there anything modifyingearningoramountbefore the return? No. Hence it will return always the sum of the values passed down toearningandamountwhen you made the initial function call.