Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jonah Shi
Jonah Shi
10,140 Points

calculate all the input number fields and put the sum into an empty input field

Hi: I'm trying to do (As above). My code is like $("#totle").val("0"); $('.row input[type=number]').val("0");

        function calc(){
        var cateringOrderNumber = 0;    
        $('.catering input[type=number]').each(function(i,n){

            $(n).defaultValue = 0;
            cateringOrderNumber += parseInt($(n).val(),10);
            console.log(cateringOrderNumber);
        });
        return cateringOrderNumber;

        $("#totle").val(cateringOrderNumber*4.90);

        }

        $('.catering input[type=number]').keyup(function(){
            calc();
        });

but my cateringOrderNumber always returns to 0, I set the defalut value, but still no good luck.

Anyone can help?

3 Answers

Steven Parker
Steven Parker
229,732 Points

Your statements are in the wrong order.

The statement that would put the calculated value into your "totle" element comes after the return statement of the function.

The return statement ends the function so nothing after it will ever be executed.

So always make sure that return is the last thing a function does.

Jonah Shi
Jonah Shi
10,140 Points

Thank you Steven, that does make sense. I'm quite new in Javascript. Here is another question.

I'm trying to add 2 parts prices together, and I did a test, it gives me weird number like 50.300000000000004, but I've already add parseFloat, why this still happens?

Here is my code. $("#totle").val("0"); $('.row input[type=number]').val("0");

        //1. calculate Catering Part
        function calcCatering(){
            $('.catering input[type=number]').each(function(){
                $(this).defaultValue = 0;
            });
            var cateringTotlePrice = 0;

            var fruitsaladpailsQty       = parseInt($('input[name="saladfruitpails"]').val(),10);
            var muffinsQty               = parseInt($('input[name="muffins"]').val(),10);
            var sourdoughsandwichesQty   = parseInt($('input[name="sandwiches"]').val(),10);
            var assortedwrapsQty         = parseInt($('input[name="wraps"]').val(),10);
            var fruitplateQty            = parseInt($('input[name="minipails"]').val(),10);
            var selectionsaladregularQty = parseInt($('input[name="saladregular"]').val(),10);
            var selectionsaladlargeQty   = parseInt($('input[name="saladlarger"]').val(),10);
            var slidersQty               = parseInt($('input[name="sliders"]').val(),10);

            cateringTotlePrice = parseFloat(fruitsaladpailsQty*4.90 + 
                                 muffinsQty*4.90 +
                                 sourdoughsandwichesQty*8.90 +
                                 assortedwrapsQty*9.50 +
                                 fruitplateQty *60.00 +
                                 selectionsaladregularQty*8.90 +
                                 selectionsaladlargeQty*11.90 +
                                 slidersQty*4.90, 10);

            //console.log(cateringTotlePrice);
            return cateringTotlePrice;

        }


        //2. calculate sandwiches Part
        function calcSandwiches(){
            var sandwichesTotlePrice = 0;
            var sandwichesOrderNumber = 0;  
            $('.sandwiches input[type=number]').each(function(){

                $(this).defaultValue = 0;
                sandwichesOrderNumber += parseInt($(this).val(),10);
                console.log(sandwichesOrderNumber);
            });
            sandwichesTotlePrice = parseFloat(sandwichesOrderNumber*8.90,10);
            console.log(sandwichesTotlePrice);
            return sandwichesTotlePrice;
        }

        function calcTotle(){
            $("#totle").val(calcCatering()+calcSandwiches());
        }



        $('.sandwiches input[type=number]').keyup(function(){
            calcTotle();
        });
Steven Parker
Steven Parker
229,732 Points

You don't need parseFloat, that's for converting strings into numbers, and you already have numbers.

Float math is known to introduce small errors, this is typical behavior and generally considered insignificant except, of course, for display.

But right before you output your final total, you can convert it to a string and trim it 2 (or some nmber of) decimal places:

    $("#totle").val((calcCatering()+calcSandwiches()).toFixed(2));

In future, create new questions for different issues so other students reading the forum can find the information more easily.

Jonah Shi
Jonah Shi
10,140 Points

Thank you, I wrote almost everything, and when I test it, it has some problems. Will open up another post, thanks again.