If statement help.....

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    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.queryst ring("Quantity" ) %>;

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


    Appreciate your help.


    David
  • Mick White

    #2
    Re: If statement help.....

    David wrote:
    [snip][color=blue]
    >
    > if x.value = 0, then y.value = 0 else
    > Run function below......
    >
    > _______________ _______________ ____
    > var squantity = <%= request.queryst ring("Quantity" ) %>;
    >
    > function doCalc(x,y){
    > y.value = 1*x.value + 1*squantity - 1;
    > }[/color]

    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

    Comment

    • Michael Winter

      #3
      Re: If statement help.....

      On Thu, 18 Nov 2004 17:34:48 GMT, Mick White <mwhite13@roche ster.rr.com>
      wrote:

      [snip]
      [color=blue]
      > I'm not sure if "0" and 0 are considered equal,[/color]

      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.

      Comment

      • Mick White

        #4
        Re: If statement help.....

        Michael Winter wrote:[color=blue]
        > On Thu, 18 Nov 2004 17:34:48 GMT, Mick White
        > <mwhite13@roche ster.rr.com> wrote:
        >
        > [snip]
        >[color=green]
        >> I'm not sure if "0" and 0 are considered equal,[/color]
        >
        >
        > 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]
        >[/color]

        Thanks, Michael.
        Mick

        Comment

        • RobG

          #5
          Re: If statement help.....

          David wrote:[color=blue]
          > Hi,
          >
          > I have the following script given to me by a very helpful person on
          > this group.[/color]
          [...]

          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.

          Comment

          • Steve van Dongen

            #6
            Re: If statement help.....

            RobG <rgqld@iinet.ne t.auau> wrote:
            [color=blue]
            >David wrote:[color=green]
            >> Hi,
            >>
            >> I have the following script given to me by a very helpful person on
            >> this group.[/color]
            >[...]
            >
            > 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';[/color]

            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

            Comment

            • RobG

              #7
              Re: If statement help.....

              Steve van Dongen wrote:[color=blue]
              > RobG <rgqld@iinet.ne t.auau> wrote:[/color]
              [...][color=blue][color=green]
              >> 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';[/color]
              >
              >
              > 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.
              >[/color]

              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.

              Comment

              • David Gordon

                #8
                Re: If statement help.....


                Mick,

                Your a true genius.....work s 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!

                Comment

                • Dr John Stockton

                  #9
                  Re: If statement help.....

                  JRS: In article <Xfbnd.775$I52. 27861@news.optu s.net.au>, dated Fri, 19
                  Nov 2004 00:38:15, seen in news:comp.lang. javascript, RobG
                  <rgqld@iinet.ne t.auau> posted :[color=blue]
                  >
                  > 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';[/color]


                  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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                  <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                  Comment

                  • Michael Winter

                    #10
                    Re: If statement help.....

                    On Fri, 19 Nov 2004 17:42:06 +1000, RobG <rgqld@iinet.ne t.auau> wrote:
                    [color=blue]
                    > Steve van Dongen wrote:
                    >[color=green]
                    >> RobG <rgqld@iinet.ne t.auau> wrote:
                    >>[color=darkred]
                    >>> Also, doing arithmetic operations on variables will cause leading
                    >>> zeros to be dropped, e.g.
                    >>>
                    >>> var a = 05,[/color][/color][/color]

                    You didn't actually write that as a string literal. :P
                    [color=blue][color=green][color=darkred]
                    >>> b = 1,
                    >>> c = b*a; // c = '5', not '05';[/color]
                    >>
                    >> 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.[/color][/color]

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

                    DecimalIntegerL iteral ::
                    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.
                    [color=blue]
                    > As far as I can tell, that will only happen if you use parseInt
                    > incorrectly, e.g.:
                    >
                    > var a = parseInt(010),[/color]

                    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.

                    Comment

                    Working...