473,394 Members | 1,765 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,394 software developers and data experts.

If statement help.....

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
Jul 23 '05 #1
9 1401
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
Jul 23 '05 #2
On Thu, 18 Nov 2004 17:34:48 GMT, Mick White <mw******@rochester.rr.com>
wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
Michael Winter wrote:
On Thu, 18 Nov 2004 17:34:48 GMT, Mick White
<mw******@rochester.rr.com> wrote:

[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
Jul 23 '05 #4
David wrote:
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.
Jul 23 '05 #5
RobG <rg***@iinet.net.auau> wrote:
David wrote:
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
Jul 23 '05 #6
Steve van Dongen wrote:
RobG <rg***@iinet.net.auau> wrote:

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


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.
Jul 23 '05 #7

Mick,

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

I wish I could get a grasp on Javascript.....Doh !
Thanks again for your help.
David
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #8
JRS: In article <Xf*****************@news.optus.net.au>, dated Fri, 19
Nov 2004 00:38:15, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :

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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #9
On Fri, 19 Nov 2004 17:42:06 +1000, RobG <rg***@iinet.net.auau> wrote:
Steve van Dongen wrote:
RobG <rg***@iinet.net.auau> wrote:
Also, doing arithmetic operations on variables will cause leading
zeros to be dropped, e.g.

var a = 05,
You didn't actually write that as a string literal. :P
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.


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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #10

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

Similar topics

3
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: ...
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
2
by: Little PussyCat | last post by:
Hello, I hope you can help me. We have a SQL Server 2000 database at work, (which works with a VB6 frontend) which grew to a considerable size, so one of my past colleagues sent me this...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
11
by: Scott C. Reynolds | last post by:
In VB6 you could do a SELECT CASE that would evaluate each case for truth and execute those statements, such as: SELECT CASE True case x > y: dosomestuff() case x = 5: dosomestuff() case y >...
10
by: John Smith | last post by:
Can you do a Select Statement within a Select Statement? I want to build a query similar to queries built in Access which link to other queries but using only SQL Statements. Is it possible? If...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
4
by: Jack | last post by:
Hi, I have a asp page where part of the code is as follows. This builds up the sql statement partially. sql01 = "UPDATE EquipmentTbl SET " sql01 = sql01 & "SerialNumber = '" &...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
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
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.