for循环每一行累计计算错误

for loop each row cumulative calculation wrong

function calc() {
  var aa = document.getElementById("aa").value;
  var bb = document.getElementById("bb").value;
  var cc = document.getElementById("cc").value;
  var dd = document.getElementById("dd").value / 365;

  var r = "";
  var lastTotal = Number(aa);
  
  for (var i = 0; i < dd; i++) {
  
   var firstPart = 1 + ((bb / 100) / cc);
   var secondPart = cc * dd;
   var final = Math.pow(firstPart,secondPart);
   var addition = lastTotal * final;
   var newTotal = lastTotal + addition;
   console.log(addition);

   r += i + 1 + ") " + Math.round(lastTotal) + "---" + Math.round(addition) + "---" + Math.round(newTotal) + "<br/>";
   r += "";
   
   lastTotal = newTotal;
  }

  document.getElementById("table").innerHTML += r;
 }
<div> AA - <input type="text" id="aa" value="12000" /></div>
      <div> BB - <input type="text" id="bb" value="20" /></div>
      <div> CC - <input type="text" id="cc" value="12" /></div>
      <div> DD - <input type="text" id="dd" value="1825" /></div>
      <div> <input type="button" value="Get" onclick="calc();" /></div>
      <br/><br/>
   <div id="output"></div>
   <br/><br/>
   <div id="table"></div>

AA is total number of quantities
BB is percentage
CC is no of times in a year (1 to 12)
DD is total years

输出为:

1) 12000---32352---44352
2) 44352---119571---163922
3) 163922---441930---605852
4) 605852---1633359---2239211
5) 2239211---6036847---8276058

但是,输出应该是:

1) 12000---2633---14633
2) 14633---3210---17843
3) 17843---3915---21758
4) 21758---4774---26532
5) 26532---5821---32353

我正在尝试使用以下公式获得所需的结果。

AA (1 + ((BB/100)/CC)) ^ (CC*DD)

我从 Math.round(lastTotal) & Math.round(newTotal) 得到了正确的结果。但是,Math.round(addition) 是问题所在。我不确定,我哪里弄错了..

问题是您试图打印出每年的信息,但您正在计算总年份的总和 (5)

是循环决定了多少年,你只需要每次计算一年就可以了。

我的方法是计算一年的期末余额

从第一笔存款中减去它,您将获得赚取的利息

function calc() {
  var aa = document.getElementById("aa").value;
  var bb = document.getElementById("bb").value;
  var cc = document.getElementById("cc").value;
  var dd = document.getElementById("dd").value;

  let totalYears = dd / 365;
  let rate = bb / 100;
  let intrest = aa;

  for (let i = 0; i < totalYears; i++) {
    // first calculate the end balance
    let endBalance = Math.round((intrest * Math.pow((1 + (rate / cc)), cc * 1)));
    // substract the end balance from the first deposit later on the deposit will become the interest because we repalce it
    let interestErned = endBalance - intrest;


    // just my way of printing
    let p = document.createElement('p');
    p.innerHTML = `${(i+1)}) ${intrest}--${interestErned}--${endBalance}`;
    document.querySelector('#table').appendChild(p);

    // override the first deposit by the end balance so we can calculate for the next year and so on
    intrest = endBalance;
  }
}
<div> AA - <input type="text" id="aa" value="12000" /></div>
<div> BB - <input type="text" id="bb" value="20" /></div>
<div> CC - <input type="text" id="cc" value="12" /></div>
<div> DD - <input type="text" id="dd" value="1825" /></div>
<div> <input type="button" value="Get" onclick="calc();" /></div>
<br/><br/>
<div id="output"></div>
<br/><br/>
<div id="table"></div>