0

I have a weird problem with some operations. I'm trying to calculate total items quantity just like shopping carts but sometimes the result shown is a Nan.

var thisID = $(this).attr('id');

var itemname = $(this).find('div .name').html();
var itemprice = $(this).find('div .price').html();

if (include(Arrays, thisID)) {
    var price = $('#each-' + thisID).children("li").children(".shopp-price").find('em').html();
    var quantity = $('#each-' + thisID).children("li").children(".shopp-quantity").html();
        quantity = parseInt(quantity) + parseInt(1);

        var total = (parseFloat(itemprice) * parseFloat(quantity)).toFixed(2);

        $('#each-' + thisID).children("li").children(".shopp-price").find('em').html(total);
        $('#each-' + thisID).children("li").children(".shopp-quantity").html(quantity);

        var prev_charges = $('.cart-total').html();
        prev_charges = parseFloat(prev_charges) - parseFloat(price);

        prev_charges = (parseFloat(prev_charges) + parseFloat(total)).toFixed(2);
        $('.cart-total').html(prev_charges);
        qte = prev_charges;

} 

The problem is sometimes it shows well and other times not. I am wondering if this is a parseFloat() or parseInt() issue. Here is a screenshot https://www.screencast.com/t/B4LtcaOD6J5

David Wesson
  • 25
  • 1
  • 7
  • Don't show the full code, try to show only the operations, else nobody will read. – Thomas Rbt May 03 '17 at 12:18
  • `parseInt(1)` I guess you are really making sure that number is a number. ;) Well something in the html() is causing parseInt/parseFloat to fail. – epascarello May 03 '17 at 12:18
  • 1
    Your impulse that it is related to `parseFloat` or `parseInt` is correct-- you are almost certainly, at some point, passing them values that don't parse nicely to numbers. Try console logging everything they get passed. Also, don't forget the `radix` argument for [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt). [Here's why](http://stackoverflow.com/questions/6611824/why-do-we-need-to-use-radix). – Alexander Nied May 03 '17 at 12:19

0 Answers0