# Thread: a prob with a bit of code :S

1. Registered User
Join Date
Mar 2009
Posts
12

## a prob with a bit of code :S

function processOrder()
{
function getSub_Values()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables

var shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
var pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
var item1Total = parseFloat(document.Order.Item1SubTotal.value);
var item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
var cleaningTotal = shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

}

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(cleaningTotal, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = cleaningTotal + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75&#37; of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = cleaningTotal.toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}
}

it's givin me an error: cleaningTotal is undefined.... i tink it's a prob with local and global var's but not sure how to do it, it has to b in that method format tho so i dno how to clean it up so that it works... :S ne1 b able to point me in the rite direction? it's attached to buttons in a form too but think it's only cleaningtotal thats givin me the prob...
Last edited by Astereal; 04-23-2009 at 04:33 AM.

2. In javascript, the keyword "VAR" creates a variable with limited scope. If it is used within a function, then it exists only inside the function and is destroyed when the function finishes running each time. If you declare a variable anywhere in the script without the keyword "VAR", then javascript will create a global variable that persists throughout the script.

A local "VAR" allows you to write functions that can be used recursively without destroying some global variable.

BTW, variables defined in the "FUNCTION" header are also local to the function, just as if you declared them using the "VAR" keyword inside the function.

3. Registered User
Join Date
Mar 2009
Posts
12
Originally Posted by gil davis
In javascript, the keyword "VAR" creates a variable with limited scope. If it is used within a function, then it exists only inside the function and is destroyed when the function finishes running each time. If you declare a variable anywhere in the script without the keyword "VAR", then javascript will create a global variable that persists throughout the script.

A local "VAR" allows you to write functions that can be used recursively without destroying some global variable.

BTW, variables defined in the "FUNCTION" header are also local to the function, just as if you declared them using the "VAR" keyword inside the function.
yer i no. thats my prob tbh lol, is it poss to make a global variable inside a function or how do i get it so that the cleanin total value appears outside that function to use for the tax and order total etc. thats the prob that's escapin me! ty for reply tho m8

4. Registered User
Join Date
Mar 2009
Posts
12
ne1? :S

5. Registered User+
Join Date
Aug 2007
Posts
3,767
Leave out the var, or declare it in the scope you want it.

6. Registered User
Join Date
Mar 2009
Posts
12
Originally Posted by Declan1991
Leave out the var, or declare it in the scope you want it.
i dont follow ya tbh m8... wud ya b able to point it out in the code?
been messin with it for hrs and cant sort it in the way it needs to b

7. Registered User+
Join Date
Aug 2007
Posts
3,767
Just say
Code:
`cleaningTotal = ...`
Code:
`var cleaningTotal = ...`
The other option I'd prefer is
Code:
```function processOrder()
{
var cleaningTotal;
function getSub_Values()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables

var shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
var pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
var item1Total = parseFloat(document.Order.Item1SubTotal.value);
var item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
cleaningTotal = shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

}

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(cleaningTotal, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = cleaningTotal + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75&#37; of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = cleaningTotal.toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}
}```
I think so anyway, there seems to be an extra { in the code, but maybe it's just a copying mistake or something.

8. Registered User
Join Date
Mar 2009
Posts
12
Originally Posted by Declan1991
Just say
Code:
`cleaningTotal = ...`
Code:
`var cleaningTotal = ...`
The other option I'd prefer is
Code:
```function processOrder()
{
var cleaningTotal;
function getSub_Values()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables

var shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
var pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
var item1Total = parseFloat(document.Order.Item1SubTotal.value);
var item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
cleaningTotal = shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

}

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(cleaningTotal, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = cleaningTotal + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75% of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = cleaningTotal.toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}
}```
I think so anyway, there seems to be an extra { in the code, but maybe it's just a copying mistake or something.
cheers for the reply, it says that there's somethin undefined and null in the code which aint... arg this is gna drive me nuts lol. is there a way to return the cleaningtotal value? that mite work?

9. Registered User+
Join Date
Aug 2007
Posts
3,767
Well it all depends on when these functions are being called. Since it seems to me that you aren't calling it when you should, this will probably work.
Code:
```function processOrder()
{
function getSub_Values()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables

var shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
var pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
var item1Total = parseFloat(document.Order.Item1SubTotal.value);
var item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
return shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

}

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(cleaningTotal, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = cleaningTotal + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75&#37; of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = getSub_Values().toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}
}```
Your code is all over the place it seems to me. You don't need all those inner functions as far as I can see. If you get rid of them and just put the code into the processOrder function, you'll solve all those problems, and probably speed it up very slightly too.

10. Registered User
Join Date
Mar 2009
Posts
12
Originally Posted by Declan1991
Well it all depends on when these functions are being called. Since it seems to me that you aren't calling it when you should, this will probably work.
Code:
```function processOrder()
{
function getSub_Values()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables

var shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
var pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
var item1Total = parseFloat(document.Order.Item1SubTotal.value);
var item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
return shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

}

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(cleaningTotal, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = cleaningTotal + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75% of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = getSub_Values().toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}
}```
Your code is all over the place it seems to me. You don't need all those inner functions as far as I can see. If you get rid of them and just put the code into the processOrder function, you'll solve all those problems, and probably speed it up very slightly too.
yer, tbh i'm just gna leave it the way it was now, too much trouble makin this subtotal method, cheers for ur time tho m8 rly appreciate it

11. Registered User+
Join Date
Aug 2007
Posts
3,767
I don't think you understand. All the inner functions are doing is slowing it down, and complicating it.
Code:
```function processOrder()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables
shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
item1Total = parseFloat(document.Order.Item1SubTotal.value);
item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
var gross =  shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(gross, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = gross + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75&#37; of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = gross.toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}```
That is functionally the same. I haven't checked it fully, that's up to you, but I don't spot any bugs from a glance. It still is badly thought out, unclear and over-complicated. Functions should be planned in advance, and in JavaScript, it is extremely rare that they contain more functions. I'd go as far as saying that's bad practice in JavaScript. The function calculateTaxAmount only makes it difficult to follow. It cannot be accessed from outside the function, so there is no reason for it to be there.

You need to learn how variable work. If you declare them with var, you never use var again. You never code.
Code:
```var a;
var a = 4;```

12. Registered User
Join Date
Mar 2009
Posts
12
Originally Posted by Declan1991
I don't think you understand. All the inner functions are doing is slowing it down, and complicating it.
Code:
```function processOrder()
{
var shirtsTotal, pantsTotal, item1Total,item2Total; //defines the 4 variables
shirtsTotal = parseFloat(document.Order.ShirtSubTotal.value); //gets the subtotals for each option from the above processItems method in decimal form.
pantsTotal = parseFloat(document.Order.PantsSubTotal.value);
item1Total = parseFloat(document.Order.Item1SubTotal.value);
item2Total = parseFloat(document.Order.txtItem2SubTotal.value);
var gross =  shirtsTotal + pantsTotal + item1Total + item2Total; // adds all the subtotals to give a gross total.

var taxRate = parseFloat(document.Order.txtTaxRate.value); //finds the tax amount that was assigned
var taxAmount = calculateTaxAmount(gross, taxRate); //uses the calculateTaxAmount to generate the total tax
var orderTotal = gross + taxAmount; // adds the tax to the gross total to give the net total.

function calculateTaxAmount(price, tax)
{
var amount = price * tax / 100; // gets the tax amount by multiplying the price by the tax and dividing by 100, ie 5.75% of the price
return amount; //returns the finished tax amount
}

document.Order.txtCleaningTotal.value = gross.toFixed(2); //prints the gross cleaning total in its place to 2 decimal places
document.Order.txtTaxAmount.value = taxAmount.toFixed(2); //prints the tax amount in its place in the table to 2 decimal places
document.Order.txtOrderTotal.value = orderTotal.toFixed(2); //prints the overall total in its place in the table to 2 decimal places.
}```
That is functionally the same. I haven't checked it fully, that's up to you, but I don't spot any bugs from a glance. It still is badly thought out, unclear and over-complicated. Functions should be planned in advance, and in JavaScript, it is extremely rare that they contain more functions. I'd go as far as saying that's bad practice in JavaScript. The function calculateTaxAmount only makes it difficult to follow. It cannot be accessed from outside the function, so there is no reason for it to be there.

You need to learn how variable work. If you declare them with var, you never use var again. You never code.
Code:
```var a;
var a = 4;```
yer i no, but i also sed in my first post that it had to b in that format, it was in the assignment specifications lol. so therefore it has to b thataway. >< nps tho tbh, i'll just submit the workin version and say cudnt get the other way workin

13. Registered User+
Join Date
Aug 2007
Posts
3,767
That's not a great attitude, it's not complicated. Just learn about scope, or otherwise, learn how to use the return statement.

And unless you find it amusing that you couldn't find another way to do it, I don't know why you would put "lol" after your sentences.

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.18167 seconds
• Memory Usage 3,008KB
• Queries Executed 15 (?)
Template Usage (34):
• (12)bbcode_code
• (5)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (13)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (13)postbit
• (13)postbit_onlinestatus
• (13)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (26):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (70):
• init_startup
• friendlyurl_resolve_class
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete