Search code examples
javascriptjquerysubtotal

Calculate all item totals to subtotal Jquery


Note: bunch of people have had similar questions but my code is set up a little differently so those solutions won't work here.

I have a subtotal variable that should add each total(price * quantity) for each item and return the combined total. I'm getting the subtotal is coming back as: 8.99undefinedundefinedundefined. If I add a new item it's now: 8.9911.99undefinedundefined

Here's my code:

// defining Order Items
// ea Item creates a new row with 3 td cells. Includes Item description and Price
var $burger = $("<tr><td class='addedBurger'>Royale w/ Cheese</td><td></td><td>$8.99</td> <td id='burgerCount'></td> </tr>");
var $pizza = $("<tr><td class='addedPizza'>Arugula Pizza</td><td></td><td>$11.99</td> <td id='pizzaCount'></td> </tr>");
var $pork = $("<tr><td class='addedPork'>Smoked Pork Ribs</td><td></td><td>$14.99</td> <td id='porkCount'></td> </tr>");
var $icecream = $("<tr><td class='addedIceCream'>Ice Cream Biscuit</td><td></td><td>$7.99</td> <td id='icecreamCount'></td> </tr>");

// Tbody(Order Items get prepended to here)
var $tbody = $('#placeOrderHere');


// BURGER
var $burgerTotal;
var $burgerCounter = 0;
var $burgerPrice = 8.99;
// Add to Order: Burger Button
$( "#place-order1" ).click(function() {
// adds burger item table row
$burger.prependTo($("#placeOrderHere"));
// counts burgers
$burgerCounter ++;
// updates burger quanities in row td
$('#burgerCount').text($burgerCounter);

$burgerTotal = ($burgerCounter * $burgerPrice).toFixed(2);
$orderSubTotal = ($burgerTotal + $pizzaTotal + $porkTotal + $icecreamTotal);
$('#subtotal').text($orderSubTotal);
});

// Defining Subtotal
var $orderSubTotal;

//$orderSubtotal = $burgerTotal + $pizzaTotal + $porkTotal +   $icecreamTotal;
// console.log($orderSubtotal);

To keep this post shorter. I only included one of the four items - Each items code is the same. Only the names are different... So Pizza has the variables $pizzaTotal, $pizzaCounter, $pizzaPrice etc.,

This part is working I just can't seem to get the subtotaling to work correctly. I had a different problem w/ subtotaling before I put the tofixed on the $itemTotal. It was adding the total but giving me Nan issues if I had more than two items subtotaling...

What the best fix? Thanks in advance!!! :)


Solution

  • I believe you're having a "typecasting" issue due to math on uninitialized variables.

    Try adding this to the top of your codeblock-

    $burgerTotal = 0;
    $pizzaTotal = 0;
    $porkTotal = 0;
    $icecreamTotal = 0; 
    

    But you are also getting strings rather than ints somewhere. This also needs to be corrected.

    For example

    var a = 10; var b; a + b 
    //returns NaN
    
    var a = 10; var b; a.toString() + b 
    //returns "10undefined" 
    

    Edit- I believe I found it. .toFixed() turns your int into a string. Either change the floating point with $orderSubTotal or change

     $burgerTotal = ($burgerCounter * $burgerPrice).toFixed(2);
    

    to

    $burgerTotal = parseFloat(($burgerCounter * $burgerPrice).toFixed(2));