Help needed with a numeric more than function.

Discussion in 'Javascript' started by Paul Taylor, Jan 16, 2004.

  1. Paul Taylor

    Paul Taylor Guest

    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.value * 12000 * 0.1));
    form.Amt0.value = AddDecimal(SAmt0);

    Thanks.
     
    Paul Taylor, Jan 16, 2004
    #1
    1. Advertisements

  2. Paul Taylor

    DJ WIce Guest

    : 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.Number.value);
    input_price_pounds = 120.00;
    input_price_pennies = input_price_pounds * 100;
    TotalPayPennies = Math.round( (input_number * input_price_pennies *
    (input_number <= 20 ? 1: 0.9 ) ) );

    form.Amt0.value = AddDecimal(TotalPayPennies);


    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
     
    DJ WIce, Jan 16, 2004
    #2
    1. Advertisements

  3. Richard Cornford, Jan 16, 2004
    #3
  4. 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_pounds is useless. It would make more sense to simply use a
    literal in its place and add a comment next to input_price_pennies (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(document.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_name.control_name certainly does not.

    The previous example would look like:

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

    Mike
     
    Michael Winter, Jan 16, 2004
    #4
  5. Paul Taylor

    Randy Webb Guest

    function getValue(control){return +control);

    is more efficient than the Number.
    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
    When used with a *name*, where does document.formName.controlName fail?

    document.formID.controlID is known to fail in some browsers, but not the
    name.
     
    Randy Webb, Jan 16, 2004
    #5
  6. The difference is, more than likely, negligible.
    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].
    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, Jan 16, 2004
    #6
  7. Paul Taylor

    DJ WIce Guest

    Maybe just visit:
    http://javascript.internet.com/calculators/best-price.html

    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


    : 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.value * 12000 * 0.1));
    : form.Amt0.value = AddDecimal(SAmt0);
    :
    : Thanks.
     
    DJ WIce, Jan 16, 2004
    #7
  8. Paul Taylor

    Randy Webb Guest

    Michael Winter wrote:

    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.
    There is something to be said for clarity.
    Yes. Mozilla balked on it when using an ID. Since I never give my
    forms/fields ID's, its not an issue to me. :)
    With names, it can't. With ID's, it can obviously be argued that the
    collections syntax is better since it fails in Mozilla.

    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 Webb, Jan 16, 2004
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.