Help needed with a numeric more than function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Taylor

    #1

    Help needed with a numeric more than function.

    I want to show a figure based on an numeric entry that more than 20
    (or 21and higher).

    If Number value is 21 or higher then the amount returned is Number
    value * 120 (£) * 10 (%) to give a discount on 21 and higher.

    How to I add that function to this line of code.

    SAmt0 = Math.round(eval (form.Number.va lue * 12000 * 0.1));
    form.Amt0.value = AddDecimal(SAmt 0);

    Thanks.
  • DJ WIce

    #2
    Re: Help needed with a numeric more than function.


    : If Number value is 21 or higher then the amount returned is Number
    : value * 120 (£) * 10 (%) to give a discount on 21 and higher.

    input_number = eval(form.Numbe r.value);
    input_price_pou nds = 120.00;
    input_price_pen nies = input_price_pou nds * 100;
    TotalPayPennies = Math.round( (input_number * input_price_pen nies *
    (input_number <= 20 ? 1: 0.9 ) ) );

    form.Amt0.value = AddDecimal(Tota lPayPennies);


    Here the 0.9 stands for 10% discound and 1 for normal price.
    And 20 of cause stands for the max. # of items where you get no discount.


    I hope this helps.
    Wouter


    Comment

    • Richard Cornford

      #3
      Re: Help needed with a numeric more than function.

      "DJ WIce" <contextmenu@dj wice.com> wrote in message
      news:bu8t2s$agh $1@news.tudelft .nl...[color=blue]
      >
      >:If Number value is 21 or higher then the amount returned is Number
      >:value * 120 (£) * 10 (%) to give a discount on 21 and higher.
      >
      > input_number = eval(form.Numbe r.value);[/color]
      <snip>

      <URL: http://jibbering.com/faq/#FAQ4_40 >

      - and -

      <URL: http://jibbering.com/faq/#FAQ4_21 >

      Richard.


      Comment

      • Michael Winter

        #4
        Re: Help needed with a numeric more than function.

        On Fri, 16 Jan 2004 15:39:51 +0100, DJ WIce <contextmenu@dj wice.com> wrote:
        [color=blue]
        > : If Number value is 21 or higher then the amount returned is Number
        > : value * 120 (£) * 10 (%) to give a discount on 21 and higher.
        >
        > input_number = eval(form.Numbe r.value);
        > input_price_pou nds = 120.00;
        > input_price_pen nies = input_price_pou nds * 100;
        > TotalPayPennies = Math.round( (input_number * input_price_pen nies *
        > (input_number <= 20 ? 1: 0.9 ) ) );
        >
        > form.Amt0.value = AddDecimal(Tota lPayPennies);
        >
        > Here the 0.9 stands for 10% discound and 1 for normal price.
        > And 20 of cause stands for the max. # of items where you get no discount.
        >
        > I hope this helps.[/color]

        I doubt it will:

        1) All the variables above are global (Bad Idea [TM]),
        2) You use eval() for no particular reason,
        3) input_price_pou nds is useless. It would make more sense to simply use a
        literal in its place and add a comment next to input_price_pen nies (this
        is obviously a minor point), and
        4) You don't validate that the value is in fact a number and branch if
        it's not.

        To the OP: it would be nice to know where this magic £120 comes from. It's
        much easier to write a function when one knows exactly what it's supposed
        to do and why (have the 'do', but not the 'why').

        A typical discount function might look like:

        function applyDiscount( number, level, discount ) {
        if( number > level ) {
        return number * ( 1 - discount / 100 );
        }
        return number;
        }

        This could be called like so:

        // price (in pounds) has already been
        // declared and contains a number (not a String)

        price = applyDiscount( price, 20, 10 );

        Here, if 'price' is greater than £20, a 10% discount is applied
        (multiplied by 0.9). However, your 'magic' £120 confuses the issue here.

        In the meantime, to get a (valid) value from a control, you could use this:

        function getValue( control ) {
        return Number( control.value );
        }

        If the form control contains a valid number, that number is returned. If
        it contains text (or some other erroneous value), it will return NaN. To
        test for the latter, use the isNaN() function.

        This could be called like so:

        var price = getValue(docume nt.forms['your_form'].elements['cost']);

        if( isNaN( price )) {
        // The 'cost' field in the 'your_form' form contained text
        // (is not a number)
        } else {
        // The 'cost' field contained a valid number
        }

        If you can clear up the £120 business, I might be able to provide a full
        solution.

        One last point, when you access form controls, it's better to use what I
        call, for lack of a better name, the 'Collection syntax'. It should work
        across every browser - document.form_n ame.control_nam e certainly does not.

        The previous example would look like:

        document.forms['form_name'].elements['control_name']

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • Randy Webb

          #5
          Re: Help needed with a numeric more than function.

          Michael Winter wrote:
          [color=blue]
          > On Fri, 16 Jan 2004 15:39:51 +0100, DJ WIce <contextmenu@dj wice.com> wrote:
          >[color=green]
          >> : If Number value is 21 or higher then the amount returned is Number
          >> : value * 120 (£) * 10 (%) to give a discount on 21 and higher.
          >>
          >> input_number = eval(form.Numbe r.value);
          >> input_price_pou nds = 120.00;
          >> input_price_pen nies = input_price_pou nds * 100;
          >> TotalPayPennies = Math.round( (input_number * input_price_pen nies *
          >> (input_number <= 20 ? 1: 0.9 ) ) );
          >>
          >> form.Amt0.value = AddDecimal(Tota lPayPennies);
          >>
          >> Here the 0.9 stands for 10% discound and 1 for normal price.
          >> And 20 of cause stands for the max. # of items where you get no discount.
          >>
          >> I hope this helps.[/color]
          >
          >
          > I doubt it will:
          >
          > 1) All the variables above are global (Bad Idea [TM]),
          > 2) You use eval() for no particular reason,
          > 3) input_price_pou nds is useless. It would make more sense to simply use
          > a literal in its place and add a comment next to input_price_pen nies
          > (this is obviously a minor point), and
          > 4) You don't validate that the value is in fact a number and branch if
          > it's not.
          >
          > To the OP: it would be nice to know where this magic £120 comes from.
          > It's much easier to write a function when one knows exactly what it's
          > supposed to do and why (have the 'do', but not the 'why').
          >
          > A typical discount function might look like:
          >
          > function applyDiscount( number, level, discount ) {
          > if( number > level ) {
          > return number * ( 1 - discount / 100 );
          > }
          > return number;
          > }
          >
          > This could be called like so:
          >
          > // price (in pounds) has already been
          > // declared and contains a number (not a String)
          >
          > price = applyDiscount( price, 20, 10 );
          >
          > Here, if 'price' is greater than £20, a 10% discount is applied
          > (multiplied by 0.9). However, your 'magic' £120 confuses the issue here.
          >
          > In the meantime, to get a (valid) value from a control, you could use this:
          >
          > function getValue( control ) {
          > return Number( control.value );
          > }[/color]

          function getValue(contro l){return +control);

          is more efficient than the Number.
          [color=blue]
          > If the form control contains a valid number, that number is returned. If
          > it contains text (or some other erroneous value), it will return NaN. To
          > test for the latter, use the isNaN() function.
          >
          > This could be called like so:
          >
          > var price = getValue(docume nt.forms['your_form'].elements['cost']);[/color]

          will return a reference to the form object, not to its value and hence
          price will always be NaN.

          var price = +document.forms['your_form'].elements['cost'].value
          [color=blue]
          > if( isNaN( price )) {
          > // The 'cost' field in the 'your_form' form contained text
          > // (is not a number)
          > } else {
          > // The 'cost' field contained a valid number
          > }
          >
          > If you can clear up the £120 business, I might be able to provide a full
          > solution.
          >
          > One last point, when you access form controls, it's better to use what I
          > call, for lack of a better name, the 'Collection syntax'. It should work
          > across every browser - document.form_n ame.control_nam e certainly does not.[/color]

          When used with a *name*, where does document.formNa me.controlName fail?

          document.formID .controlID is known to fail in some browsers, but not the
          name.


          --
          Randy

          Comment

          • Michael Winter

            #6
            Re: Help needed with a numeric more than function.

            On Fri, 16 Jan 2004 13:16:09 -0500, Randy Webb <hikksnotathome @aol.com>
            wrote:
            [color=blue]
            > Michael Winter wrote:
            >[color=green]
            >> function getValue( control ) {
            >> return Number( control.value );
            >> }[/color]
            >
            > function getValue(contro l){return +control);
            >
            > is more efficient than the Number.[/color]

            The difference is, more than likely, negligible.
            [color=blue][color=green]
            >> If the form control contains a valid number, that number is returned.
            >> If it contains text (or some other erroneous value), it will return
            >> NaN. To test for the latter, use the isNaN() function.
            >>
            >> This could be called like so:
            >>
            >> var price = getValue(docume nt.forms['your_form'].elements['cost']);[/color]
            >
            > will return a reference to the form object, not to its value and hence
            > price will always be NaN.[/color]

            I know. The function accesses the value using the reference. I couldn't
            decide whether to have a string or object reference.

            From a Software Engineering perspective, it should be the string; only
            pass what's necessary.

            From a size perspective, using the object reduces the number of characters
            necessary by six (6n - 6, actually) on each call. Granted, using + instead
            of Number() reduces it further, but I prefer the clarity of the latter. To
            me, the former still seems like operator abuse[1].
            [color=blue][color=green]
            >> One last point, when you access form controls, it's better to use what
            >> I call, for lack of a better name, the 'Collection syntax'. It should
            >> work across every browser - document.form_n ame.control_nam e certainly
            >> does not.[/color]
            >
            > When used with a *name*, where does document.formNa me.controlName fail?
            >
            > document.formID .controlID is known to fail in some browsers, but not the
            > name.[/color]

            To be honest, I forget the answer to this question (you raised it before,
            and I think Mr Nielsen gave the answer). As I said last time, it cannot be
            argued that the collection syntax will succeed with more reliability than
            the shortcut method, no matter what identification method (id vs name) is
            used. I think it far more sensible to use one reliable method that works
            in all cases, than to switch between two depending on the situation.

            Mike


            [1] It isn't. ECMA-262 defines unary + as an operator that "converts its
            operand to Number type." Strangely, Netscape's Core JavaScript reference
            (any version 1.3+, certainly) doesn't even acknowledge the existance of a
            unary + operator.

            --
            Michael Winter
            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            • DJ WIce

              #7
              Re: Help needed with a numeric more than function.

              Maybe just visit:


              Although the discount function like Michael Winter presented:

              function applyDiscount( number, level, discount ) {
              if( number > level ) {
              return number * ( 1 - discount / 100 );
              }
              return number;
              }

              would be enough I think.
              Wouter


              "Paul Taylor" <itchyfeetorigi nal@hotmail.com > wrote in message
              news:de595b7f.0 401160541.36295 822@posting.goo gle.com...
              : I want to show a figure based on an numeric entry that more than 20
              : (or 21and higher).
              :
              : If Number value is 21 or higher then the amount returned is Number
              : value * 120 (£) * 10 (%) to give a discount on 21 and higher.
              :
              : How to I add that function to this line of code.
              :
              : SAmt0 = Math.round(eval (form.Number.va lue * 12000 * 0.1));
              : form.Amt0.value = AddDecimal(SAmt 0);
              :
              : Thanks.


              Comment

              • Randy Webb

                #8
                Re: Help needed with a numeric more than function.

                Michael Winter wrote:

                <--snip-->
                [color=blue][color=green]
                >> function getValue(contro l){return +control);
                >>
                >> is more efficient than the Number.[/color]
                >
                >
                > The difference is, more than likely, negligible.[/color]

                Probably so. Dending on how many times you call it. To call it only
                once, there is no real difference in these:

                eval(control)
                Number(control)
                +control

                Or any of the others, depending on how many times you do it. IIRC, it
                took us upwards of a million iterations to see a real difference.
                [color=blue][color=green][color=darkred]
                >>> If the form control contains a valid number, that number is returned.
                >>> If it contains text (or some other erroneous value), it will return
                >>> NaN. To test for the latter, use the isNaN() function.
                >>>
                >>> This could be called like so:
                >>>
                >>> var price = getValue(docume nt.forms['your_form'].elements['cost']);[/color]
                >>
                >>
                >> will return a reference to the form object, not to its value and hence
                >> price will always be NaN.[/color]
                >
                >
                > I know. The function accesses the value using the reference. I couldn't
                > decide whether to have a string or object reference.[/color]

                Ooops.
                [color=blue]
                > From a Software Engineering perspective, it should be the string; only
                > pass what's necessary.
                >
                > From a size perspective, using the object reduces the number of
                > characters necessary by six (6n - 6, actually) on each call. Granted,
                > using + instead of Number() reduces it further, but I prefer the clarity
                > of the latter. To me, the former still seems like operator abuse[1].[/color]

                There is something to be said for clarity.
                [color=blue][color=green][color=darkred]
                >>> One last point, when you access form controls, it's better to use
                >>> what I call, for lack of a better name, the 'Collection syntax'. It
                >>> should work across every browser - document.form_n ame.control_nam e
                >>> certainly does not.[/color]
                >>
                >>
                >> When used with a *name*, where does document.formNa me.controlName fail?
                >>
                >> document.formID .controlID is known to fail in some browsers, but not
                >> the name.[/color]
                >
                >
                > To be honest, I forget the answer to this question (you raised it
                > before, and I think Mr Nielsen gave the answer).[/color]

                Yes. Mozilla balked on it when using an ID. Since I never give my
                forms/fields ID's, its not an issue to me. :-)
                [color=blue]
                > As I said last time, it cannot be argued that the collection syntax
                > will succeed with more reliability than the shortcut method, no matter
                > what identification method (id vs name) is used.[/color]

                With names, it can't. With ID's, it can obviously be argued that the
                collections syntax is better since it fails in Mozilla.

                [color=blue]
                > I think it far more sensible to use one reliable method that works
                > in all cases, than to switch between two depending on the situation.[/color]

                The only time the shortcut method doesn't work is if you are using ID's,
                and if you are going to use ID's instead of names (which breaks the page
                in NN4 anyway), then just use getElementById (thats what its for,
                right?). And keep it the same method?


                --
                Randy

                Comment

                Working...