Help needed with a numeric more than function.

P

Paul Taylor

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.
 
D

DJ WIce

: 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
 
M

Michael Winter

: 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.

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
 
R

Randy Webb

Michael said:
: 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.


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 );
}

function getValue(control){return +control);

is more efficient than the Number.
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']);

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
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.

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.
 
M

Michael Winter

function getValue(control){return +control);

is more efficient than the Number.

The difference is, more than likely, negligible.
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']);

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

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].
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.

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.
 
D

DJ WIce

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.
 
R

Randy Webb

Michael Winter wrote:

The difference is, more than likely, negligible.

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.
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']);


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


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

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].

There is something to be said for clarity.
To be honest, I forget the answer to this question (you raised it
before, and I think Mr Nielsen gave the answer).

Yes. Mozilla balked on it when using an ID. Since I never give my
forms/fields ID's, its not an issue to me. :)
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.

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

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.

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?
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top