If statement help.....

D

David

Hi,

I have the following script given to me by a very helpful person on
this group.
It basically copies the contents of Textbox 1 on my form to Textbox 2
with some adjustments.

What I now want to do is, if Textbox 1 is entered as 0 (zero), then I
want textbox2 to display 0 (zero) also.

I need to say something like,

if x.value = 0, then y.value = 0 else
Run function below......

__________________________________
var squantity = <%= request.querystring("Quantity") %>;

function doCalc(x,y){
y.value = 1*x.value + 1*squantity - 1;
}
___________________________________


Appreciate your help.


David
 
M

Mick White

David wrote:
[snip]
if x.value = 0, then y.value = 0 else
Run function below......

__________________________________
var squantity = <%= request.querystring("Quantity") %>;

function doCalc(x,y){
y.value = 1*x.value + 1*squantity - 1;
}

function doCalc(x,y){
y.value = x.value==0?0:1*x.value + (squantity - 1);
}

I'm not sure if "0" and 0 are considered equal, though, And you are
relying on the user to input entries, I would check this.
Mick
 
M

Michael Winter

[snip]
I'm not sure if "0" and 0 are considered equal,

They are as long as you don't perform a strict equality (===) comparison.
When a string and a number are compared, the string will be converted to a
number (even if that results in NaN).

[snip]

Mike
 
M

Mick White

Michael said:
On Thu, 18 Nov 2004 17:34:48 GMT, Mick White

[snip]
I'm not sure if "0" and 0 are considered equal,


They are as long as you don't perform a strict equality (===)
comparison. When a string and a number are compared, the string will be
converted to a number (even if that results in NaN).

[snip]

Thanks, Michael.
Mick
 
R

RobG

David said:
Hi,

I have the following script given to me by a very helpful person on
this group.
[...]

RobB also provided a regular expression test to ensure the the input
consisted of digits only that you should probably include if the input
is expected to be only digits.

Also, doing arithmetic operations on variables will cause leading zeros
to be dropped, e.g.

var a = 05,
b = 1,
c = b*a; // c = '5', not '05';

Cheers, Rob.
 
S

Steve van Dongen

RobG said:
David said:
Hi,

I have the following script given to me by a very helpful person on
this group.
[...]

RobB also provided a regular expression test to ensure the the input
consisted of digits only that you should probably include if the input
is expected to be only digits.

Also, doing arithmetic operations on variables will cause leading zeros
to be dropped, e.g.

var a = 05,
b = 1,
c = b*a; // c = '5', not '05';

Yes, but don't use leading zeros unless you mean to use octal. 05 is
5, but 09 is an error, and 010 is 8.

Regards,
Steve
 
R

RobG

Steve said:
Yes, but don't use leading zeros unless you mean to use octal. 05 is
5, but 09 is an error, and 010 is 8.

As far as I can tell, that will only happen if you use parseInt
incorrectly, e.g.:

var a = parseInt(010),
b = 1,
c = b*a; // c = '8', not '010';

Also, making a either 08 or 09 will return '0'.

However this is easily remedied by using parseInt correctly with a
radix value of 10, forcing the result to be base 10, not base 8:

var a = parseInt(010,10),
b = 1,
c = b*a; // c = '10', not '010';


Cheers, Rob.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 19
Nov 2004 00:38:15, seen in RobG
Also, doing arithmetic operations on variables will cause leading zeros
to be dropped, e.g.

var a = 05,
b = 1,
c = b*a; // c = '5', not '05';


That's misleading. Variables a, b, c are of type Number, represented as
IEEE Doubles, and the concept of leading zero does not apply. Since c
is not a String, the comment on that line makes no sense.

var a = 09
would be valid, giving type Number of value nine. AFAIR, only function
parseInt(St) interprets leading zero as implying Octal, and
parseInt('09') does not give a javascript error but is a confusing way
of getting Number 0.

While multiplying by one does cause String-to-Number conversion, it is
inefficient, and a sign of having learnt only from the inadequate. The
unary + sign is better; see FAQ 4.21.

var a = "088" ; b = +a ; // b = Number 88
but var a = "0x88" ; b = +a ; // b = Number 136, Hex 88
and var a = 0x88 ; b = a ; // b = Number 136, Hex 88
 
D

David Gordon

Mick,

Your a true genius.....works a treat.

I wish I could get a grasp on Javascript.....Doh !


Thanks again for your help.


David
 
M

Michael Winter

You didn't actually write that as a string literal. :p

In a string, possibly. In a numeric literal, it's technically illegal. The
grammar for the decimal variant of a numeric literal is:

DecimalIntegerLiteral ::
0
NonZeroDigit DecimalDigits[opt]

In other words, zero, or a non-zero digit followed by any number
(including zero) of digits (including zero). So

0 and 10

are fine, but

01

is not. You may not find any implementations that choke on this, but it
should be avoided. As far as octal literals are concerned, there's no such
thing in ECMAScript, only hexadecimal and decimal.
As far as I can tell, that will only happen if you use parseInt
incorrectly, e.g.:

var a = parseInt(010),

Again, you forgot the quotes. As shown, the (technically illegal) number,
10, will be converted to a string, '10', and parsed with no issues.
However,

parseInt('010')

*may* be different. As far as ECMAScript is concerned, it's up to the
implementation to decide if the literal above is octal or decimal. Most
will probably choose octal to follow precedent, but you shouldn't rely on
it. The only thing you can rely on is that '0x' will automatically be
treated as hexadecimal if no radix is specified, and a string starting
with a non-zero digit will be decimal.

An alternative approach is to use the unary plus (+) operator. Assuming
that you're not using parseInt to ignore illegal characters, using unary
plus will suffice. The rules here are:

- An empty string, or all whitespace, is zero.

From now on, leading and trailing whitespace is ignored:

- '0x' or '0X', followed by any hexadecimal digit is parsed as hex.
- 'Infinity' is, well, guess. You can optionally prefix it with
plus (+) or minus (-).
- Decimal values can be any of

1.2e3 or 1.2 or 1.e3
.1e2 or .1
1e2 or 1

'e' can be upper- or lowercase, and all leading zeros are
ignored, so

01 and 1

are equivalent. Of course, either the exponent or integer sections
can be signed:

-1.2 +2 .6e-9 +1.E+5

- Any other patterns or characters results in NaN.

[snip]

Mike
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top