## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

# 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?

## 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.

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 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 slidersQty               = parseInt(\$('input[name="sliders"]').val(),10);

muffinsQty*4.90 +
sourdoughsandwichesQty*8.90 +
assortedwrapsQty*9.50 +
fruitplateQty *60.00 +
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();
});
```

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.

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