www.webdeveloper.com
Results 1 to 13 of 13

Thread: a prob with a bit of code :S

  1. #1
    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% 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. #2
    Join Date
    Nov 2002
    Posts
    4,473
    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. #3
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by gil davis View Post
    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. #4
    Join Date
    Mar 2009
    Posts
    12
    ne1? :S

  5. #5
    Join Date
    Aug 2007
    Posts
    3,767
    Leave out the var, or declare it in the scope you want it.
    Great wit and madness are near allied, and fine a line their bounds divide.

  6. #6
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by Declan1991 View Post
    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. #7
    Join Date
    Aug 2007
    Posts
    3,767
    Just say
    Code:
    cleaningTotal = ...
    Instead of
    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.
    Great wit and madness are near allied, and fine a line their bounds divide.

  8. #8
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by Declan1991 View Post
    Just say
    Code:
    cleaningTotal = ...
    Instead of
    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. #9
    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% 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.

  10. #10
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by Declan1991 View Post
    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. #11
    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% 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;
    gil_davis told you this initially. And please the Message Posting Guidelines and Please help us to help you, they are stickies, at the top of every page in this forum!
    Great wit and madness are near allied, and fine a line their bounds divide.

  12. #12
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by Declan1991 View Post
    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;
    gil_davis told you this initially. And please the Message Posting Guidelines and Please help us to help you, they are stickies, at the top of every page in this forum!
    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. #13
    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.
    Great wit and madness are near allied, and fine a line their bounds divide.

Thread Information

Users Browsing this Thread

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
  •  
HTML5 Development Center



Recent Articles