Javascript Miscalculation and $NaN










0















I'm confused and I've tried getting the following javascript to render a Total amount calculation but I'm having $NaN and some cell are not displaying the correct values when a dropdown list is used to pick a mailing option.



Here's the Javascript and the JSFIDDLE that I've been working on:



$(function() 
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;

$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".prod-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".prod-row .item-cost").html("$" + shipCost.toFixed(2));
$(".prod-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);


Desired outcome



To have all the items in only 1 row and where total column shows the following math result from the calculation: (Qty. + ItemCost + Tax) + (Qty.*S&H).



Thanks for any help!










share|improve this question






















  • Shouldn't it be (Qty. * ItemCost + Tax)...?

    – zer00ne
    Nov 15 '18 at 22:33












  • Can't see .costperitem selector in your fiddle in which case it would be undefined

    – Steve O'Connor
    Nov 15 '18 at 22:34











  • shippingcost is not a defined index, see here: t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);. It appears to be defined as shipcost

    – Twisty
    Nov 15 '18 at 23:35












  • I've made some changes, but I still don't understand why all S&H is not changing by the total is. S&H should display the total cost for shipping and handling, while Total column should display the sum of all the costs in the row. Any help?

    – Marcelo Martins
    Nov 16 '18 at 0:17















0















I'm confused and I've tried getting the following javascript to render a Total amount calculation but I'm having $NaN and some cell are not displaying the correct values when a dropdown list is used to pick a mailing option.



Here's the Javascript and the JSFIDDLE that I've been working on:



$(function() 
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;

$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".prod-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".prod-row .item-cost").html("$" + shipCost.toFixed(2));
$(".prod-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);


Desired outcome



To have all the items in only 1 row and where total column shows the following math result from the calculation: (Qty. + ItemCost + Tax) + (Qty.*S&H).



Thanks for any help!










share|improve this question






















  • Shouldn't it be (Qty. * ItemCost + Tax)...?

    – zer00ne
    Nov 15 '18 at 22:33












  • Can't see .costperitem selector in your fiddle in which case it would be undefined

    – Steve O'Connor
    Nov 15 '18 at 22:34











  • shippingcost is not a defined index, see here: t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);. It appears to be defined as shipcost

    – Twisty
    Nov 15 '18 at 23:35












  • I've made some changes, but I still don't understand why all S&H is not changing by the total is. S&H should display the total cost for shipping and handling, while Total column should display the sum of all the costs in the row. Any help?

    – Marcelo Martins
    Nov 16 '18 at 0:17













0












0








0








I'm confused and I've tried getting the following javascript to render a Total amount calculation but I'm having $NaN and some cell are not displaying the correct values when a dropdown list is used to pick a mailing option.



Here's the Javascript and the JSFIDDLE that I've been working on:



$(function() 
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;

$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".prod-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".prod-row .item-cost").html("$" + shipCost.toFixed(2));
$(".prod-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);


Desired outcome



To have all the items in only 1 row and where total column shows the following math result from the calculation: (Qty. + ItemCost + Tax) + (Qty.*S&H).



Thanks for any help!










share|improve this question














I'm confused and I've tried getting the following javascript to render a Total amount calculation but I'm having $NaN and some cell are not displaying the correct values when a dropdown list is used to pick a mailing option.



Here's the Javascript and the JSFIDDLE that I've been working on:



$(function() 
function calcTotal()
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;

$("#shippingmethod").change(function(e)
var shipQty = parseInt($(".prod-row .qty").text());
var shipCost = parseFloat($(this).val());
$(".prod-row .item-cost").html("$" + shipCost.toFixed(2));
$(".prod-row .total").html("$" + (shipQty * shipCost).toFixed(2));
var total = calcTotal();
$("#product-totals .total").html("$" + total.toFixed(2));
);
);


Desired outcome



To have all the items in only 1 row and where total column shows the following math result from the calculation: (Qty. + ItemCost + Tax) + (Qty.*S&H).



Thanks for any help!







javascript jquery jquery-ui twitter-bootstrap-3 mathematical-optimization






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 22:18









Marcelo MartinsMarcelo Martins

859




859












  • Shouldn't it be (Qty. * ItemCost + Tax)...?

    – zer00ne
    Nov 15 '18 at 22:33












  • Can't see .costperitem selector in your fiddle in which case it would be undefined

    – Steve O'Connor
    Nov 15 '18 at 22:34











  • shippingcost is not a defined index, see here: t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);. It appears to be defined as shipcost

    – Twisty
    Nov 15 '18 at 23:35












  • I've made some changes, but I still don't understand why all S&H is not changing by the total is. S&H should display the total cost for shipping and handling, while Total column should display the sum of all the costs in the row. Any help?

    – Marcelo Martins
    Nov 16 '18 at 0:17

















  • Shouldn't it be (Qty. * ItemCost + Tax)...?

    – zer00ne
    Nov 15 '18 at 22:33












  • Can't see .costperitem selector in your fiddle in which case it would be undefined

    – Steve O'Connor
    Nov 15 '18 at 22:34











  • shippingcost is not a defined index, see here: t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);. It appears to be defined as shipcost

    – Twisty
    Nov 15 '18 at 23:35












  • I've made some changes, but I still don't understand why all S&H is not changing by the total is. S&H should display the total cost for shipping and handling, while Total column should display the sum of all the costs in the row. Any help?

    – Marcelo Martins
    Nov 16 '18 at 0:17
















Shouldn't it be (Qty. * ItemCost + Tax)...?

– zer00ne
Nov 15 '18 at 22:33






Shouldn't it be (Qty. * ItemCost + Tax)...?

– zer00ne
Nov 15 '18 at 22:33














Can't see .costperitem selector in your fiddle in which case it would be undefined

– Steve O'Connor
Nov 15 '18 at 22:34





Can't see .costperitem selector in your fiddle in which case it would be undefined

– Steve O'Connor
Nov 15 '18 at 22:34













shippingcost is not a defined index, see here: t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);. It appears to be defined as shipcost

– Twisty
Nov 15 '18 at 23:35






shippingcost is not a defined index, see here: t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);. It appears to be defined as shipcost

– Twisty
Nov 15 '18 at 23:35














I've made some changes, but I still don't understand why all S&H is not changing by the total is. S&H should display the total cost for shipping and handling, while Total column should display the sum of all the costs in the row. Any help?

– Marcelo Martins
Nov 16 '18 at 0:17





I've made some changes, but I still don't understand why all S&H is not changing by the total is. S&H should display the total cost for shipping and handling, while Total column should display the sum of all the costs in the row. Any help?

– Marcelo Martins
Nov 16 '18 at 0:17












1 Answer
1






active

oldest

votes


















1














The issue resides in the index call. In the following function:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;



You reference p.shippingcost which does not exist and returns undefined.



Also, you want to parseFloat() on your costperitem section. parseInt() will round to the nearest Integer.



Also you need to slice() off any preceding $ signs.



Correct like so:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseFloat($(".prod-row .costperitem").text().trim().slice(1)),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shipcost);

console.log(p, t);
return t;



Good Luck!



Update



I would suggest the following code that is fixed to perform all the various steps needed with the correct Class Names in place.



$(function() 
function toCur(fl)
return "$" + parseFloat(fl).toFixed(2);


function parseCurToFloat(txt)
return parseFloat(txt.trim().slice(1));


function setNewShipCost($row, cost)
$row.find(".shipping-cost").html(toCur(cost));
return toCur(cost);


function calcRowTotal($row)
var q = parseInt($row.find(".qty").text());
var c = parseCurToFloat($row.find(".item-cost").text());
var t = parseCurToFloat($row.find(".tax").text());
var sh = parseCurToFloat($row.find(".shipping-cost").text());
var ttl = (q * c) + (q * sh) + t;
$row.find(".total").html(toCur(ttl));
return ttl;


function calcNewTotal()
var t = 0.00;
$(".prod-row").each(function(ind, el)
t = t + parseCurToFloat($(el).find(".total").text());
);
$("#product-totals .total").html(toCur(t));
return t;


$("#shippingmethod").change(function(e)
var shipCost = $(this).val();
console.log("Set Ship", setNewShipCost($(".prod-row"), shipCost));
console.log("Calc Row Total", calcRowTotal($(".prod-row")));
console.log("Calc total", calcNewTotal());
);
);


Working Example: http://jsfiddle.net/Twisty/ag1u4vjc/






share|improve this answer

























  • You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

    – Marcelo Martins
    Nov 16 '18 at 0:04











  • @MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

    – Twisty
    Nov 16 '18 at 0:49











  • I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

    – Marcelo Martins
    Nov 16 '18 at 16:26











  • I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

    – Marcelo Martins
    Nov 16 '18 at 20:34






  • 1





    @MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

    – Twisty
    Nov 17 '18 at 1:52











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328677%2fjavascript-miscalculation-and-nan%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The issue resides in the index call. In the following function:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;



You reference p.shippingcost which does not exist and returns undefined.



Also, you want to parseFloat() on your costperitem section. parseInt() will round to the nearest Integer.



Also you need to slice() off any preceding $ signs.



Correct like so:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseFloat($(".prod-row .costperitem").text().trim().slice(1)),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shipcost);

console.log(p, t);
return t;



Good Luck!



Update



I would suggest the following code that is fixed to perform all the various steps needed with the correct Class Names in place.



$(function() 
function toCur(fl)
return "$" + parseFloat(fl).toFixed(2);


function parseCurToFloat(txt)
return parseFloat(txt.trim().slice(1));


function setNewShipCost($row, cost)
$row.find(".shipping-cost").html(toCur(cost));
return toCur(cost);


function calcRowTotal($row)
var q = parseInt($row.find(".qty").text());
var c = parseCurToFloat($row.find(".item-cost").text());
var t = parseCurToFloat($row.find(".tax").text());
var sh = parseCurToFloat($row.find(".shipping-cost").text());
var ttl = (q * c) + (q * sh) + t;
$row.find(".total").html(toCur(ttl));
return ttl;


function calcNewTotal()
var t = 0.00;
$(".prod-row").each(function(ind, el)
t = t + parseCurToFloat($(el).find(".total").text());
);
$("#product-totals .total").html(toCur(t));
return t;


$("#shippingmethod").change(function(e)
var shipCost = $(this).val();
console.log("Set Ship", setNewShipCost($(".prod-row"), shipCost));
console.log("Calc Row Total", calcRowTotal($(".prod-row")));
console.log("Calc total", calcNewTotal());
);
);


Working Example: http://jsfiddle.net/Twisty/ag1u4vjc/






share|improve this answer

























  • You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

    – Marcelo Martins
    Nov 16 '18 at 0:04











  • @MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

    – Twisty
    Nov 16 '18 at 0:49











  • I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

    – Marcelo Martins
    Nov 16 '18 at 16:26











  • I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

    – Marcelo Martins
    Nov 16 '18 at 20:34






  • 1





    @MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

    – Twisty
    Nov 17 '18 at 1:52
















1














The issue resides in the index call. In the following function:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;



You reference p.shippingcost which does not exist and returns undefined.



Also, you want to parseFloat() on your costperitem section. parseInt() will round to the nearest Integer.



Also you need to slice() off any preceding $ signs.



Correct like so:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseFloat($(".prod-row .costperitem").text().trim().slice(1)),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shipcost);

console.log(p, t);
return t;



Good Luck!



Update



I would suggest the following code that is fixed to perform all the various steps needed with the correct Class Names in place.



$(function() 
function toCur(fl)
return "$" + parseFloat(fl).toFixed(2);


function parseCurToFloat(txt)
return parseFloat(txt.trim().slice(1));


function setNewShipCost($row, cost)
$row.find(".shipping-cost").html(toCur(cost));
return toCur(cost);


function calcRowTotal($row)
var q = parseInt($row.find(".qty").text());
var c = parseCurToFloat($row.find(".item-cost").text());
var t = parseCurToFloat($row.find(".tax").text());
var sh = parseCurToFloat($row.find(".shipping-cost").text());
var ttl = (q * c) + (q * sh) + t;
$row.find(".total").html(toCur(ttl));
return ttl;


function calcNewTotal()
var t = 0.00;
$(".prod-row").each(function(ind, el)
t = t + parseCurToFloat($(el).find(".total").text());
);
$("#product-totals .total").html(toCur(t));
return t;


$("#shippingmethod").change(function(e)
var shipCost = $(this).val();
console.log("Set Ship", setNewShipCost($(".prod-row"), shipCost));
console.log("Calc Row Total", calcRowTotal($(".prod-row")));
console.log("Calc total", calcNewTotal());
);
);


Working Example: http://jsfiddle.net/Twisty/ag1u4vjc/






share|improve this answer

























  • You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

    – Marcelo Martins
    Nov 16 '18 at 0:04











  • @MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

    – Twisty
    Nov 16 '18 at 0:49











  • I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

    – Marcelo Martins
    Nov 16 '18 at 16:26











  • I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

    – Marcelo Martins
    Nov 16 '18 at 20:34






  • 1





    @MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

    – Twisty
    Nov 17 '18 at 1:52














1












1








1







The issue resides in the index call. In the following function:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;



You reference p.shippingcost which does not exist and returns undefined.



Also, you want to parseFloat() on your costperitem section. parseInt() will round to the nearest Integer.



Also you need to slice() off any preceding $ signs.



Correct like so:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseFloat($(".prod-row .costperitem").text().trim().slice(1)),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shipcost);

console.log(p, t);
return t;



Good Luck!



Update



I would suggest the following code that is fixed to perform all the various steps needed with the correct Class Names in place.



$(function() 
function toCur(fl)
return "$" + parseFloat(fl).toFixed(2);


function parseCurToFloat(txt)
return parseFloat(txt.trim().slice(1));


function setNewShipCost($row, cost)
$row.find(".shipping-cost").html(toCur(cost));
return toCur(cost);


function calcRowTotal($row)
var q = parseInt($row.find(".qty").text());
var c = parseCurToFloat($row.find(".item-cost").text());
var t = parseCurToFloat($row.find(".tax").text());
var sh = parseCurToFloat($row.find(".shipping-cost").text());
var ttl = (q * c) + (q * sh) + t;
$row.find(".total").html(toCur(ttl));
return ttl;


function calcNewTotal()
var t = 0.00;
$(".prod-row").each(function(ind, el)
t = t + parseCurToFloat($(el).find(".total").text());
);
$("#product-totals .total").html(toCur(t));
return t;


$("#shippingmethod").change(function(e)
var shipCost = $(this).val();
console.log("Set Ship", setNewShipCost($(".prod-row"), shipCost));
console.log("Calc Row Total", calcRowTotal($(".prod-row")));
console.log("Calc total", calcNewTotal());
);
);


Working Example: http://jsfiddle.net/Twisty/ag1u4vjc/






share|improve this answer















The issue resides in the index call. In the following function:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseInt($(".prod-row .costperitem").text().trim()),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shippingcost);

console.log(p, t);
return t;



You reference p.shippingcost which does not exist and returns undefined.



Also, you want to parseFloat() on your costperitem section. parseInt() will round to the nearest Integer.



Also you need to slice() off any preceding $ signs.



Correct like so:



function calcTotal() 
var p =
qty: parseInt($(".prod-row .qty").text().trim()),
costperitem: parseFloat($(".prod-row .costperitem").text().trim().slice(1)),
tax: parseFloat($(".prod-row .tax").text().trim().slice(1)),
shipcost: parseFloat($(".prod-row .shippingcost").text().trim().slice(1)),
price: parseFloat($(".prod-row .item-cost").text().trim().slice(1))
;
var t = 0.00;
if (p)
t = (p.qty * p.costperitem) + (p.tax) + (p.qty * p.shipcost);

console.log(p, t);
return t;



Good Luck!



Update



I would suggest the following code that is fixed to perform all the various steps needed with the correct Class Names in place.



$(function() 
function toCur(fl)
return "$" + parseFloat(fl).toFixed(2);


function parseCurToFloat(txt)
return parseFloat(txt.trim().slice(1));


function setNewShipCost($row, cost)
$row.find(".shipping-cost").html(toCur(cost));
return toCur(cost);


function calcRowTotal($row)
var q = parseInt($row.find(".qty").text());
var c = parseCurToFloat($row.find(".item-cost").text());
var t = parseCurToFloat($row.find(".tax").text());
var sh = parseCurToFloat($row.find(".shipping-cost").text());
var ttl = (q * c) + (q * sh) + t;
$row.find(".total").html(toCur(ttl));
return ttl;


function calcNewTotal()
var t = 0.00;
$(".prod-row").each(function(ind, el)
t = t + parseCurToFloat($(el).find(".total").text());
);
$("#product-totals .total").html(toCur(t));
return t;


$("#shippingmethod").change(function(e)
var shipCost = $(this).val();
console.log("Set Ship", setNewShipCost($(".prod-row"), shipCost));
console.log("Calc Row Total", calcRowTotal($(".prod-row")));
console.log("Calc total", calcNewTotal());
);
);


Working Example: http://jsfiddle.net/Twisty/ag1u4vjc/







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 1:48

























answered Nov 15 '18 at 23:41









TwistyTwisty

14.3k11534




14.3k11534












  • You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

    – Marcelo Martins
    Nov 16 '18 at 0:04











  • @MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

    – Twisty
    Nov 16 '18 at 0:49











  • I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

    – Marcelo Martins
    Nov 16 '18 at 16:26











  • I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

    – Marcelo Martins
    Nov 16 '18 at 20:34






  • 1





    @MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

    – Twisty
    Nov 17 '18 at 1:52


















  • You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

    – Marcelo Martins
    Nov 16 '18 at 0:04











  • @MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

    – Twisty
    Nov 16 '18 at 0:49











  • I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

    – Marcelo Martins
    Nov 16 '18 at 16:26











  • I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

    – Marcelo Martins
    Nov 16 '18 at 20:34






  • 1





    @MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

    – Twisty
    Nov 17 '18 at 1:52

















You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

– Marcelo Martins
Nov 16 '18 at 0:04





You've helped me creating this script! When the select goes to "Select an option", which should be the default then the S&H should be $0.00. But instead I somehow messed up your previous script and the Item Cost is inheriting the default value of $0.00 and the total still shows $NaN.

– Marcelo Martins
Nov 16 '18 at 0:04













@MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

– Twisty
Nov 16 '18 at 0:49





@MarceloMartins Take a look at this: jsfiddle.net/Twisty/ag1u4vjc

– Twisty
Nov 16 '18 at 0:49













I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

– Marcelo Martins
Nov 16 '18 at 16:26





I appreciate your help Twisty. There's still the issue with S&H displaying $2.95 on page load despite the dropdown displays the default "Select an option", which has a value of 0.00. S&H only goes to 0.00 once a different selection is made. As you choose the default "Select and option" then the S&H column will display 0.00. However, that should be the default state. Can you help?

– Marcelo Martins
Nov 16 '18 at 16:26













I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

– Marcelo Martins
Nov 16 '18 at 20:34





I've made a couple of modifications at jsfiddle.net/UXEngineer/Lf9g3ax1 but I'm still having issues with the default value when the page first load.

– Marcelo Martins
Nov 16 '18 at 20:34




1




1





@MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

– Twisty
Nov 17 '18 at 1:52






@MarceloMartins the issue is a Order of Operations issue. You're calculating the total before updating the tax. jsfiddle.net/Twisty/Lf9g3ax1/73

– Twisty
Nov 17 '18 at 1:52




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328677%2fjavascript-miscalculation-and-nan%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin