JavaScript operations

1

I have a problem with this code I try to perform an operation that subtracts it from the result of two inputs is shown in another input but being empty at the beginning I mark the error NaN only gives me the result when running again the script

function calcular() {
  var n1 = parseInt(document.dosificacion.capacidad.value);
  var n3 = parseInt(document.dosificacion.fijador.value);
  var n5 = parseInt(document.dosificacion.dosificacion.value);
  // alerta
  if (isNaN(n1)) {
    alert('Ingresa el capacidad');
  } else {
    document.dosificacion.esencia.value = (n1 * n5) / (100).toFixed();
    var n2 = parseInt(document.dosificacion.esencia.value);
    document.dosificacion.esencia.value += " g.";
    document.dosificacion.fijador.value = (n1 * .08).toFixed() + " g.";
    document.dosificacion.perfumol.value = (n1 - n2 - n3).toFixed() + " g.";
  }
}
<form name="dosificacion">
  <div>
    <input class="capacidad" type="text" name="capacidad"><br>
  </div>
  <p></p>
  <div>
    <input class="esencia" type="text" name="esencia"><br>

  </div>
  <p></p>
  <div>
    <input class="perfumol" type="text" name="perfumol"><br>
  </div>
  <p></p>
  <div>
    <input class="fijador" type="text" name="fijador"><br>

  </div>
  <div>
    <p></p>
    <select class="concentracion" name="dosificacion">
      <option value="33">33</option>
      <option value="38">38</option>
      <option value="50">50</option>
    </select>
  </div>
  <p></p>
  <div>
    <input class="boton1" type="button" value="dosificacion" onclick="calcular()">
  </div>
</form>
    
asked by AQUILES BAILO BUENO 05.09.2018 в 17:33
source

2 answers

2

The problem is that you want to use the value of n3 before the input has a value, I recommend you do all your calculations with local variables and finally put them in inputs

function calcular() {
  var n1 = parseInt(document.dosificacion.capacidad.value);

  // alerta
  if (isNaN(n1)) {
    alert('Ingresa el capacidad');
    return;
  }
  
  var n5 = parseInt(document.dosificacion.dosificacion.value);
  var esencia = (n1 * n5) / (100).toFixed();
  var n2 = parseInt(esencia);
  var fijador = (n1 * .08).toFixed();
  var perfumol = (n1 - n2 - fijador).toFixed();

  document.dosificacion.esencia.value = esencia + " g.";
  document.dosificacion.fijador.value = fijador + " g.";
  document.dosificacion.perfumol.value = perfumol + " g.";

}
<form name="dosificacion">
  <div>
    <input class="capacidad" type="text" name="capacidad"><br>
  </div>
  <p></p>
  <div>
    <input class="esencia" type="text" name="esencia"><br>

  </div>
  <p></p>
  <div>
    <input class="perfumol" type="text" name="perfumol"><br>
  </div>
  <p></p>
  <div>
    <input class="fijador" type="text" name="fijador"><br>

  </div>
  <div>
    <p></p>
    <select class="concentracion" name="dosificacion">
      <option value="33">33</option>
      <option value="38">38</option>
      <option value="50">50</option>
    </select>
  </div>
  <p></p>
  <div>
    <input class="boton1" type="button" value="dosificacion" onclick="calcular()">
  </div>
</form>
    
answered by 05.09.2018 / 19:07
source
0

You can do a function to ensure that you receive numbers with JQuery

function Quita_Moneda(n){
    n=String(n);
    var s=n.replace("$","");
    while(s.toString().indexOf(",")!=-1) s=s.replace(",","");
    s=parseFloat(s);
    if(isNaN(s))s=0;
    return s;
}

var numero=Quita_Moneda($("input").val());
    
answered by 05.09.2018 в 17:35