How to add inputs dynamically and that the result appears in other input?

2

Greetings, I'm looking for a way to add the amount of an input, so many times I tell you in another input. The problem arises that I must take 20% of that amount and add it when I have previously added the amount of the input 3 times. Give an example:

quantity input: 9 input of amount: $ 10,000

First multiplication: 10,000 * 3 = 30,000

Initial amount: 10,000 * 0.2

Second multiplication: 12,000 * 3 = 36,000

Percentage second initial amount: 12,000 * 0.2

Third Multiplication: 14,400 * 3 = 43,200

Total cost: $ 109,200

To be clear, I need you to get the percentage every 3 amounts, like this: 10,000, 10,000, 10,000, 12,000, 12,000, 12,000, 14,400, 14,400, 14,400, and then add all those values, knowing that in the input that you call above, there can be values from 1-50. I'm struggling to develop the algorithm, I do not get the form, who can help me thank you very much. Until now, this is what I'm using:

  <script>
      var aumento = 0.2;
      var aumentaCada = 3;
      var cantidad = $("#num").val();
      var monto = $("#input7").val();

    for(var i=0; i< cantidad ; i++){
      if((i>0) && (i % aumentaCada)==0){
        monto = monto * (1+aumento);
      }
      var total = monto * cantidad;
    }

    $("#total").val(total);

  </script>
    
asked by 13.08.2017 в 19:42
source

2 answers

2

You must review your algorithm well, first mark on paper to create the most efficient way to perform the calculation, the other is to understand well the concept of percentages as handled. when you are clear about the algorithm you can pass this to code and make it easier.

           $(document).ready(function () {
                    $("button").click(function () {

                        var cantidad = $("#cantidad").val();
                        var monto = $("#monto").val();
                        if (cantidad !== "" && monto !== "") {
                            var residuo = cantidad % 3;
                            var multiplos = parseInt((cantidad / 3), 10);
                            var total = 0;
                            for (i = 1; i <= multiplos; i++) {
                                total += (monto * 3);
                                monto = monto * 1.2;
                            }
                            if (residuo > 0) {
                                total += (monto * residuo);
                            }
                            console.log("TOTAL:" + total);
                            $("#total").val(total);
                        }
                    });
                });

It depends on you if you want to learn or just solve an exercise. Greetings.

    
answered by 13.08.2017 в 20:43
2

I'll give you a shorter process than the first answer:

<script
			  src="https://code.jquery.com/jquery-3.2.1.min.js"
			  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
			  crossorigin="anonymous"></script>

  cantidad: <input type="number" id="cantidad" name="cantidad"><br>
  monto: <input type="number" id="monto" name="monto"><br>

  <button onclick="calcula()">Calcular</button>


  <script>
  var acumulado=parseInt(0);
  function calcula(){
	  var cantidad = parseInt($("#cantidad").val());
	  var monto = parseInt($("#monto").val());
	  var vecesPorcentaje = Math.floor(cantidad/3);
	  var vecesSobrantes = cantidad-(vecesPorcentaje*3);

	  for(var i = 0; i<vecesPorcentaje; i++){
	  		acumulado = acumulado+(monto*3);
	  		monto = monto+(monto*0.2);
	  }
	  acumulado = acumulado+(monto*vecesSobrantes);
	  alert(acumulado);
  }

  </script>

I enclose the codepen so you can check that it works: calculate percentage

    
answered by 13.08.2017 в 20:45