好吧,我已经建立了这个计算器,而且我在如何循环使用这些年来将资金投入储蓄账户并在年度付款数量结束之前增加了利息和现值。
我的问题是,每年支付1000美元是在35年内增加的,这使我的总利息减少了。当我计算以前的余额+总利息和总投资时,它也会增加现值。如何创建一个在15年后停止的循环?
我也使用this spreadsheet作为公式。
到目前为止我的代码:
var current_age = 30;
var age_retirement = 65;
var expected_annual_return = 0.06;
var annual_payment = $1000;
var annual_payments = 15;
var interest = 0;
var total_interest = 0;
var total_amount_invested = 0;
var cumulative_payments;
var amount_invested;
if (total_years_until_retirement <= annual_payments) {
var total_amount_invested = (total_years_until_retirement * annual_payment + present_value);
var total_improved_amount = total_amount_invested * annual_payment;
console.log("This is the if statement");
} else {
cumulative_payments = present_value + annual_payment;
amount_invested = cumulative_payments + annual_payment;
console.log("This is the else statement");
console.log("The total amount invested is: "+total_amount_invested);
};
for (i = 0; i <= (actual_total_years_until_retirement); i++) {
interest = present_value * expected_annual_return;
present_value = present_value + annual_payment + interest;
total_interest += interest;
};
答案 0 :(得分:0)
如果要在达到15年计数后停止循环,请将以下内容放入for循环中
使用Javascript:
//if i equals 15
if(i === 15){//begin if then
//exit the loop
return false;
}//end if then
因此,如果您想在十五年后停止添加付款,您可以执行以下操作:
示例:
for (i = 0; i <= (actual_total_years_until_retirement); i++) {//begin if then
//if the loop is not on its fifteenth iteration
if(i !== 15){//begin if then else
interest = present_value * expected_annual_return;
present_value = present_value + annual_payment + interest;
total_interest += interest;
}
else{
//perform same calculations as above the remove the annual payment
interest = present_value * expected_annual_return;
present_value = present_value + interest;
total_interest += interest;
}//end if then else
}//end for loop