循环内的javascript for循环计算

时间:2019-02-23 07:20:01

标签: javascript for-loop

JsFiddle

function myFunction() {
  diff = 4;
  var l = 12;

  b = document.getElementById("yee").value;

  var a = Number(document.getElementById("base").value);
  var aa = (a*b)/100;
  z = aa;
  var r = "";
  for (var i = 0; i < diff; i++) {
    if(i) {
      var hk = (aa * document.getElementById("per").value) / 100;
      aa+= Number(hk);
    }

    for (var j = 0; j < l; j++) {
      var d = ((z*j)*5)/12/100;
      r += j+1 +") " + aa.toFixed(2) + "---" + d.toFixed(2) + "<br/>";
    }

    r += "";
  }
  document.getElementById("demo").innerHTML += r;
}

在这里,我正在尝试根据前12个月的基本数量(10000)计算基本百分比(12)。 12个月的最终产量为1200。

在12个计数(月)之后,会将整个百分比(10)添加到基本数量(10000),然后在接下来的12个月中,从先前的基本数量(11000)计算基本百分比(12)。第二个12个月的最终输出是1320。

在12个计数(月)之后,将全部百分比(10)添加到先前的基本数量(11000),然后在接下来的12个月中,从先前的基本数量(12100)计算基本百分比(12)。第三十二个月的最终输出是1452。

到目前为止已经尝试过,

enter image description here

我期望的结果,

enter image description here

最后,按预期,我得到了第二列的结果。但是,我没有得到预期的第一列和第三列结果。我想为l * diff计算此公式((((z * j)* 5)/ 12/100),这意味着48个计数为12 * 4。但是,它每12个计数就会重置一次。如何连续计算第一列和第三列?

3 个答案:

答案 0 :(得分:1)

初始化另一个变量k,因为每次循环运行时,它都从0开始,并且您正在使用j。

function myFunction() {
  diff = 4;
  var l = 12;

  b = document.getElementById("yee").value;

  var a = Number(document.getElementById("base").value);
  var aa = (a * b) / 100;
  z = aa;
  var r = "";
  k = 0;
  m = 0;
  e = 0;
  z = 0;
  for (var i = 0; i < diff; i++) {
    if (i) {
      var hk = (aa * document.getElementById("per").value) / 100;
      aa += Number(hk);

    }
    n = m;

    for (var j = 0; j < l; j++) {

      var d = ((aa * n) * 5) / 12 / 100;
      var f  = d+z;

      r += k + 1 + ") " + aa.toFixed(2) + "---" + f.toFixed(2) + "<br/>";
      k++;
      n++;
    }
    r += "";
    m++;
    z = z + d;

  }
  document.getElementById("demo").innerHTML += r;
}
<div>
  <span>Base Quantity</span>
  <input type="text" id="base" value="10000" />
</div>
<div>
  <span>Base Percent</span>
  <input type="text" id="yee" value="12" />
</div>
<div>
  <span>Whole Percent</span>
  <input type="text" id="per" value="10" />
</div>

<button onclick="myFunction()">Click</button>

<p id="demo"></p>

答案 1 :(得分:1)

        function myFunction() {
        let diff = 4;
        let l = 12;
        let basePercent = document.getElementById("yee").value;                
        let baseQuantity = Number(document.getElementById("base").value);
        let wholePercent = document.getElementById("per").value;
        let aa = (baseQuantity * basePercent) / 100;
        let d = 0;
        let z = 0;
        let r = "";
        let monthCounter = 1;

        for (let i = 0; i < diff; i++) {
            if(i) {
                let hk = (aa * wholePercent) / 100;
                aa += Number(hk);
            }

            /*d is a constant value you keep adding every month. you need to calculate it in the outer loop and
            add it to it self in every iteration of inner loop*/
            if( i === 0) {
                d = ((aa)*5)/12/100;
            } else {
                d = ((aa*i)*5)/12/100;
            }

            for (let j = 0; j < l; j++) {
                if(i ===0 && j === 0){
                    z = 0;
                } else {
                    z += d;
                }
                r += monthCounter +") " + aa.toFixed(2) + "---" + z.toFixed(2) + "<br/>";
                ++monthCounter;
            }
            r += "";
        }
        document.getElementById("demo").innerHTML += r;
    }   

答案 2 :(得分:0)

您需要创建变量来存储已完成的循环数。一个将设置第一列,第二个将处理第三列。在内部循环结束时更新两个变量。

for (var j = 0; j < l; j++) {
  var d = (((z*j)*5)/12/100) + offset;
  r += (j+1 + (offset2*l)) +") " + aa.toFixed(2) + "---" + d.toFixed(2) + "<br/>";
}
offset2++;
offset += (((z*(j-1))*5)/12/100) + (((z*(0))*5)/12/100) + 0.5

JS Fiddle