javascript - Calculate all item totals to subtotal Jquery -


note: bunch of people have had similar questions code set little differently solutions won't work here.

i have subtotal variable should add each total(price * quantity) each item , return combined total. i'm getting subtotal coming as: 8.99undefinedundefinedundefined. if add new item it's now: 8.9911.99undefinedundefined

here's code:

// defining order items // ea item creates new row 3 td cells. includes item description , 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 prepended here) var $tbody = $('#placeorderhere');   // burger var $burgertotal; var $burgercounter = 0; var $burgerprice = 8.99; // add 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 post shorter. included 1 of 4 items - each items code same. names different... pizza has variables $pizzatotal, $pizzacounter, $pizzaprice etc.,

this part working can't seem subtotaling work correctly. had different problem w/ subtotaling before put tofixed on $itemtotal. adding total giving me nan issues if had more 2 items subtotaling...

what best fix? in advance!!! :)

i believe you're having "typecasting" issue due math on uninitialized variables.

try adding top of codeblock-

$burgertotal = 0; $pizzatotal = 0; $porktotal = 0; $icecreamtotal = 0;  

but getting strings rather ints somewhere. needs corrected.

for example

var = 10; var b; + b  //returns nan  var = 10; var b; a.tostring() + b  //returns "10undefined"  

edit- believe found it. .tofixed() turns int string. either change floating point $ordersubtotal or change

 $burgertotal = ($burgercounter * $burgerprice).tofixed(2); 

to

$burgertotal = parsefloat(($burgercounter * $burgerprice).tofixed(2)); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -