【问题标题】:Calculate multiple values ​in a loop循环计算多个值
【发布时间】:2020-12-12 14:28:01
【问题描述】:

在未来收入计算器中,我需要显示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 但有了这些数据,我发现自己迷路了。 ????

【问题讨论】:

  • 使用函数,输入年数

标签: javascript calculation


【解决方案1】:

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

// 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2016-12-22
    • 2023-03-09
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多