Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Potentially dumbest question yet!!!

Options
  • 12-04-2006 7:20pm
    #1
    Registered Users Posts: 3,803 ✭✭✭


    Okay, i feel stupid asking this javascript question, but i'm new to this, anyway here goes:

    function tax_price()
    {
    var tax = 0;

    for ( i = 0; i < top.selectedProducts.length; i++)
    {
    tax = tax + (top.selectedProducts.cost) * (0.05);
    }

    tax = roundOff(tax);
    document.order.Tax.value = tax;

    total_price();
    }

    function total_price()
    {
    var subCost = 0;

    for ( i = 0; i < top.selectedProducts.length; i++)
    {
    subCost = subCost + top.selectedProducts.cost;
    }

    tax = document.order.Tax.value;
    total = subCost + tax;
    document.order.Total.value = total;
    }

    The above 2 functions should display the total cost of any number of products in a shopping cart. And it does to an extend. For example: The cost (subCost) of 3 products is 225. The tax might be something like 17.25. Therefore the total should be 242.25. However it displays 22517.25.

    I assume that it is treating the values as strings, and i don't understand why. How do i sort out this product?

    Here is little bit more info on the code.

    selectedProducts is an array in my top page(where my frames are). This array stores all the info on selected products form the website, such as the price, qty, cost, name etc.
    The above 2 functions are called after the user leaves an input field (onBlur event ).

    I can provide more code if it is any help to you in sloving this problem.

    Thanks in advance for any help.


Comments

  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Add a load of 'alert' calls to your code to ensure the data is what you expect.
    In Firefox, use Tools/Javascript Console to watch for errors.


  • Registered Users Posts: 3,803 ✭✭✭Benzino


    It's returning all of the values coreectly, eg subCost and tax have the correct values and there are no errors been reported. It seems that it is "sticking" the 2 variables together instead of adding them together.

    It's things like this that make me hate js.


  • Closed Accounts Posts: 16,793 ✭✭✭✭Hagar


    Does the variable that holds subcost allow decimal places?
    It looks like 22500 instead of 225.00 is being added to the calculated tax figure.


  • Registered Users Posts: 3,803 ✭✭✭Benzino


    Yeah, i just did an alert and it displays figures with decimal places.


  • Closed Accounts Posts: 19 TheMoralist


    try:

    total = Number(subCost) + Number(tax);


  • Advertisement
  • Registered Users Posts: 3,803 ✭✭✭Benzino


    try:

    total = Number(subCost) + Number(tax);

    YESSSSSSSSSSS!!!! that worked:) Thank you veeeerrrrrrrrrrrrrrrrrryyyyy much, i have spent ages trying to figure that out, thanks.


Advertisement