Announcement

Collapse
No announcement yet.

javascript question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • javascript question

    I'm trying to calculate a 5% discount and show it on the product listing pages. I need the [price] to have 5% subtracted from it in javascript. Is there someone that can assist me with this?
    (¯`v´¯)
    `*.¸.*´

    ¸.•´¸.•*¨) ¸.•*¨)
    (¸.•´ (¸.•´ .•´ ¸¸.•¨¯`•
    * P£†®ª *

    Shadow Trailers
    www.TrailerandTruckParts.com
    www.Trailer-Wheels.com
    www.iTrailerParts.com

  • #2
    I have this and thought it would work
    <script>
    var disc = 5;
    var total = "[price]";
    var totalnum = total.replace("$", "");
    {
    rounded = Math.round((total * disc)/100);
    document.write("Save $");
    document.write(rounded.toFixed(2) + " ");
    document.write("Off Your Order Today!");
    }
    </script>

    but it writes this: Save $NaN Off Your Order Today!

    Can anyone see what I'm doing wrong? I'd appreciate any assistance.
    (¯`v´¯)
    `*.¸.*´

    ¸.•´¸.•*¨) ¸.•*¨)
    (¸.•´ (¸.•´ .•´ ¸¸.•¨¯`•
    * P£†®ª *

    Shadow Trailers
    www.TrailerandTruckParts.com
    www.Trailer-Wheels.com
    www.iTrailerParts.com

    Comment


    • #3
      One bug in this line:

      Code:
      rounded = Math.round((total * disc)/100);
      You are doing the math on the version of the price that includes the dollar sign. Replace total with totalnum.

      That solved the NaN problem for me. If you still have problems I think it may be related to the curly braces that surround the last few lines. They seem kind of randomly placed and probably shouldn't be there at all.

      I'd recommend getting familiar with the Javascript Console in either Firefox or Chrome. It lets you type in code line by line and see the results in real time.

      Comment


      • #4
        Thank you! For some reason it rounds up ie the price is 77.18 and it calculates the 5% at 4.00 instead of the 3.86
        (¯`v´¯)
        `*.¸.*´

        ¸.•´¸.•*¨) ¸.•*¨)
        (¸.•´ (¸.•´ .•´ ¸¸.•¨¯`•
        * P£†®ª *

        Shadow Trailers
        www.TrailerandTruckParts.com
        www.Trailer-Wheels.com
        www.iTrailerParts.com

        Comment


        • #5
          It's because of the Math.round line, which rounds to the nearest integer.

          Replace these two lines:
          Code:
          rounded = Math.round((total * disc)/100);
          document.write(rounded.toFixed(2) + " ");
          With these two lines:
          Code:
          discount = ((totalnum * disc)/100).toFixed(2);
          document.write(discount + " ");

          Comment


          • #6
            awesome, thank you so much. if I can ever return the favor please don't hesitate to pm me!
            (¯`v´¯)
            `*.¸.*´

            ¸.•´¸.•*¨) ¸.•*¨)
            (¸.•´ (¸.•´ .•´ ¸¸.•¨¯`•
            * P£†®ª *

            Shadow Trailers
            www.TrailerandTruckParts.com
            www.Trailer-Wheels.com
            www.iTrailerParts.com

            Comment

            Working...
            X