我有一个收益计算器函数。
EarningsCalculator.prototype.computeEarning = function (rate, investedValue) {
var earnings = {};
var currentState = investedValue;
for (var i = 0; i <= 5; i++) {
earning=currentState*rate;
currentState = currentState + earning;
earnings[i] = currentState;
}
return earnings;
}
它需要一个“投资价值”,并根据给定的利率计算收益,本例中仅在5年内使用for循环进行计算。我需要计算每1年的earnings [0]
和每5年的earnings [1]
到earnings [4]
。
因此,对于投资价值为1000和利率为10%的情况,它在5年内返回以下输出:
Year1 = 1100
Year2 = 1210
Year3 = 1331
Year4 = 1464.1
Year5 = 1610.51
What I want is this
Year1 = 1100
Year5 = 1610.51
Year10 = 2593.74246
Year15 = 4177.248169
Year20 = 6727.499949