Karuppiah RK
Karuppiah RK

Reputation: 3964

javascript simple loop calculation

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

  first = 1 + ((bb / 100) / cc);
  second = cc * time;
  result = aa * Math.pow(first, second);

  bb_earn = aa * Math.pow(first, second) - aa;
  final = Number(aa) + Number(bb_earn);

  var r = "";
  var lastTotal = aa;
  for (var i = 0; i < dd; i++) {

    var newTotal = Number(lastTotal) + Number(bb_earn);
    zz = +newTotal;
    lastTotal = newTotal;

    r += i + 1 + ") " + aa + "---" + zz + "---" + final + "<br/>";
    r += "";
  }

  document.getElementById("table").innerHTML += r;
}
<div> A - <input type="text" id="aa" value="12000" /></div>
<div> B - <input type="text" id="bb" value="20" /></div>
<div> C - <input type="text" id="cc" value="1" /></div>
<div> D - <input type="text" id="dd" value="1825" /></div>
<div> <input type="button" value="Get" onclick="calc();" /></div>
<br/><br/>
<div id="table"></div>

I am trying to loop the default value, 20% of default value and sum of default value plus 20% of default value. In next row, default value should be previous final column sum value. I tried above javascript calculation to achieve the desired result. But, I messed up..

Output result is:

1) 12000---14400---14400
2) 12000---16800---14400
3) 12000---19200---14400
4) 12000---21600---14400
5) 12000---24000---14400

But, Output should be:

1) 12000---2400---14400
2) 14400---2880---17280
3) 17280---3456---20736
4) 20736---4147.20---24883.20
5) 24883.20---4976.60---29859.80

Upvotes: 0

Views: 61

Answers (2)

Thomas Ludewig
Thomas Ludewig

Reputation: 725

There is nothing wrong with the for next loop But i guess everything is wrong with your formulas.

            <!DOCTYPE HTML>

            <html>
            <head>
              <title>Untitled</title>
            </head>
            <body>
                <div> A - <input type="text" id="aa" value="12000" /></div>
                <div> B - <input type="text" id="bb" value="20" /></div>
                <div> C - <input type="text" id="cc" value="1" /></div>
                <div> D - <input type="text" id="dd" value="1825" /></div>
                <div> <input type="button" value="Get" onclick="calc();" /></div>
                <br/><br/>
                <div id="table"></div>
            <script>
            function calc(){
                  var aa = document.getElementById("aa").value*1.0;//ensure that we use numbers and not strings
                  var bb = document.getElementById("bb").value*1.0;
                  var cc = document.getElementById("cc").value*1.0;
                  var time = 1.0;
                  var dd = document.getElementById("dd").value*1 / 365;

                  first = 1 + ((bb / 100) / cc);//first = 1.2 bb 20 ,cc 1
                  second = cc * time; // 1*1=1
                  // i guess here you make a mistake or choose the wrong test datas
                  var fact=Math.pow(first, second) // fact = 1.2^1 
                  result = aa * fact; //result 14400 = 12000*1.2;

                  bb_earn = aa * fact - aa; // bb_earn = 1.2 * 12000 -12000 = .2*12000 =2400
                  final = aa + bb_earn; //final =12000 + 2400 = again 14400
                 var zz=0;
                  var r = "";
                  var lastTotal = aa;
                  for (var i = 0; i < dd; i++) {
                    // as you could see thére is by this numbers NO chance to get something like -4147.20
                    // there are NO AFTER DIGITS in this calculation
                    //based on the fact result not possible
                    var newTotal = Number(lastTotal) + Number(bb_earn);
                    zz = newTotal;
                    lastTotal = newTotal;

                    r += i + 1 + ") " + aa + "---" + zz + "---" + final + "<br/>";
                    r += "";
                  }

                  document.getElementById("table").innerHTML += r;
                }
                </script>
            </body>
            </html>

Upvotes: 0

Phoenix1355
Phoenix1355

Reputation: 1652

It's a bit hard to figure out what you're trying to achieve with the code, based on what you write. It could be written a lot more simple if you merely wanted to take the previous total and add 20% each time. You don't explain what time variable does and what the #cc element does.

Regardless of that, this should output the result you expect.

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 ratio = ((bb / 100) / cc);
    var addition = lastTotal * ratio;
    var newTotal = lastTotal + addition;

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

  document.getElementById("table").innerHTML += r;
}
<div> A - <input type="text" id="aa" value="12000" /></div>
<div> B - <input type="text" id="bb" value="20" /></div>
<div> C - <input type="text" id="cc" value="1" /></div>
<div> D - <input type="text" id="dd" value="1825" /></div>
<div> <input type="button" value="Get" onclick="calc();" /></div>
<br/><br/>
<div id="table"></div>

Upvotes: 1

Related Questions