jQuery calculator concatenates instead of adding

0

I am doing a calculator with jQuery and Bootstrap but it is not adding me well, in this case only jQuery content that is where the problem is. The rest works perfectly, but in the calculations is not going well, I have the following code:

var total = 0;

$(document).ready(function() {
  $("#boton_igual").click(function() {
    //if ($("#resultado").val().trim().length != 0) {
    $("#resultado").val(total);
    //}
  });
});

$(document).ready(function() {
  $("#boton_sumar").click(function() {
    if ($("#resultado").val().trim().length > 0) {
      total += parseInt($("#resultado").val());
      $("#resultado").val('');
    }
  });
});

$(document).ready(function() {
  $("#boton_borrar").click(function() {
    $("#resultado").val('');
    total = '';
  });
});

First I'm trying the sum but I do not know why the sum is wrong.

    
asked by josanangel 02.05.2018 в 15:46
source

1 answer

0

In the delete button, these initializing total as a string, after deleting the sum starts to concatenate the numbers, you must initialize total to 0

$(document).ready(function() {
$("#boton_borrar").click(function() {
  $("#resultado").val('');
  total = 0;
  });
});
    
answered by 02.05.2018 в 16:45