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% 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...
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.
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
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.
Great wit and madness are near allied, and fine a line their bounds divide.
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?
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.
Great wit and madness are near allied, and fine a line their bounds divide.
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
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.
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.
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
Bookmarks