473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Thanks.
Jul 20 '05 #1
7 1205

: 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
Jul 20 '05 #2
"DJ WIce" <co*********@djwice.com> wrote in message
news:bu**********@news.tudelft.nl...

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

<snip>

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

- and -

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

Richard.
Jul 20 '05 #3
On Fri, 16 Jan 2004 15:39:51 +0100, DJ WIce <co*********@djwice.com> wrote:
: 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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
Michael Winter wrote:
On Fri, 16 Jan 2004 15:39:51 +0100, DJ WIce <co*********@djwice.com> wrote:
: 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.
--
Randy

Jul 20 '05 #5
On Fri, 16 Jan 2004 13:16:09 -0500, Randy Webb <hi************@aol.com>
wrote:
Michael Winter wrote:
function getValue( control ) {
return Number( control.value );
}


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


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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #6
Maybe just visit:
http://javascript.internet.com/calcu...est-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
"Paul Taylor" <it***************@hotmail.com> wrote in message
news:de**************************@posting.google.c om...
: 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.
Jul 20 '05 #7
Michael Winter wrote:

<--snip-->
function getValue(control){return +control);

is more efficient than the Number.

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

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?
--
Randy

Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gary Ruben | last post by:
I have a class factory problem. You could say that my main problem is that I don't understand class factories. My specific problem: I have a class with several methods I've defined. To this class...
3
by: Rich | last post by:
This is what one of the tables looks like - they are all similar and there are about 60 different tables: CREATE TABLE SalesData1( smalldatetime TimeStamp, varchar(8) CustomerID, numeric S1,...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
23
by: mrvendetta1 | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 33 void StringToIntArray(const char *s1); void takeinteger(int i); int main() { char string; printf("enter a string of...
2
by: this one | last post by:
I have the following code <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="JavaScript"...
7
by: Sheldon | last post by:
Hi, I have the following loop that I think can be written to run faster in Numeric. I am currently using Numeric. range_va = main.xsize= 600 main.ysize= 600 #msgva is an (600x600) Numeric...
0
by: Teri Frederickson | last post by:
Assignment is to have user enter days in pay period with starting pay as a penny and then doubled for each day. Restriction of 19 days minimum and 22 days maximum for pay period. Want to have an...
2
by: redfog | last post by:
I need help writing some code for this procedure: Inventory – Add to Item Listing menu item (15 points). Application users will use this menu as part of the process of entering inventory...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.