在循环中计算多个值

Calculate multiple values ​in a loop

在未来收入计算器中,我需要显示5年、10年和15年累积的数据。

在这个账户中,我取每月供款的价值,在 12 个月后,我应用年度盈利能力,得出一年的最终价值。

为了获得第二年的价值,我将 12 个月总和的初始值与盈利的 12 个月的价值相加。

账号如下...

contributions = 536,06;
profitability = 4.27;
fixedYearVal = 536,06 * 12; // 6.432,72
profitabilityVal =  (profitability / 100) * fixedYearVal;
fixedYearprofitability = fixedYearVal + profitabilityVal;

有了这个,我发现了第一年盈利。 第二年的值为 (secondYear = fixedYearVal + fixedYearprofitability)。 第二年的最终金额将是

percentSecondYear = (profitability / 100) * secondYear;
finalSecondYear = percentSecondYear + secondYear;

而第三年的价值将是

thirYear = finalSecondYear + fixedYearVal;
percentthirdYear = (profitability / 100) * thirYear;
finalThirdyear = percentthirdYear + thirYear;

反正我说了,我需要5年10年15年,除了做几千行我想不出别的办法,我想过用一个for in Javascript 但是有了这个数据 boo 我发现自己迷路了。

我把东西扔在一起了。也许这可以帮助您入门。这个想法是 1) 设置一些基值 2) 将它扔到你想要计算的 n 年的循环中。 3) return 数组中的最终结果,因此您可以逐年查看

// Calculate the next year
const caclNextYear = (lastYear, fixedYearVal, profitability) => {
  const nextYr = lastYear + fixedYearVal;
  const percentSecondYear = (profitability / 100) * nextYr;
  const finalYr = percentSecondYear + nextYr;
  return finalYr;
};

// Calc years by number of years
const estimateByYears = (years) => {
  const contributions = 536.06;
  const profitability = 4.27;
  const fixedYearVal = 536.06 * 12; // 6.432,72
  const profitabilityVal =  (profitability / 100) * fixedYearVal;
  const fixedYearprofitability = fixedYearVal + profitabilityVal;
  const yearByYear = [fixedYearprofitability];

  for (let i = 1; i < years; i++) {
    let lastYr = yearByYear[yearByYear.length - 1];
    yearByYear.push(caclNextYear(lastYr, fixedYearVal, profitability));
  }
  
  return yearByYear;
};

// Call
const yearByYear = estimateByYears(5);
// yearByYear : [6707.397144, 13701.200146048799, 20993.63853628508, 28597.46404578445, 36525.97290453945
console.log('yearByYear', yearByYear);