Conditional shortcut (?:) limitations?

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

    #1

    Conditional shortcut (?:) limitations?

    I just wrote the following script for something I'm working on:


    ----------------------------------------------------------------------------
    -------------------
    <html>
    <head>
    <script type="text/javascript">
    function KP_growit(subje ct,widthto,heig htto) {
    var target = document.getEle mentById(subjec t);
    var tweenit = setInterval(gro wer,10);

    function grower() {
    (parseInt(targe t.style.width) < widthto)? target.style.wi dth =
    (parseInt(targe t.style.width) + 10) + "px";
    (parseInt(targe t.style.height) < heightto)? target.style.he ight =
    (parseInt(targe t.style.height) + 10) + "px";
    (parseInt(targe t.style.width) == widthto && parseInt(target .style.height)
    == heightto)? clearInterval(t weenit);
    }
    }
    </script>
    </head>
    <body>
    <div id="bluebox" style="position :absolute; left:173px; top:48px;
    width:100px; height:80px; z-index:1; background-color:#99CCFF;" ></div>
    <div><a href="#" onClick="KP_gro wit('bluebox',4 00,300); return false;">grow
    it</a></div>
    </body>
    </html>
    ----------------------------------------------------------------------------
    --------------------------------------------


    I noticed that the script does not work because of the conditional
    operators; however it DOES work when using the if...else format. Why is
    this? Can someone please help. Thanks.


  • F. Da Costa

    #2
    Re: Conditional shortcut (?:) limitations?

    TheKeith wrote:
    [color=blue]
    > I just wrote the following script for something I'm working on:
    >
    >
    > ----------------------------------------------------------------------------
    > -------------------
    > <html>
    > <head>
    > <script type="text/javascript">
    > function KP_growit(subje ct,widthto,heig htto) {
    > var target = document.getEle mentById(subjec t);
    > var tweenit = setInterval(gro wer,10);
    >
    > function grower() {
    > (parseInt(targe t.style.width) < widthto)? target.style.wi dth =
    > (parseInt(targe t.style.width) + 10) + "px";
    > (parseInt(targe t.style.height) < heightto)? target.style.he ight =
    > (parseInt(targe t.style.height) + 10) + "px";
    > (parseInt(targe t.style.width) == widthto && parseInt(target .style.height)
    > == heightto)? clearInterval(t weenit);
    > }
    > }[/color]
    function grower() {
    (parseInt(targe t.style.width) < widthto)? target.style.wi dth =
    (parseInt(targe t.style.width) + 10) + "px": true;
    (parseInt(targe t.style.height) < heightto)? target.style.he ight =
    (parseInt(targe t.style.height) + 10) + "px" : true;
    (parseInt(targe t.style.width) == widthto && parseInt(target .style.height)
    == heightto)? clearInterval(t weenit) : true;
    }
    }[color=blue]
    > </script>
    > </head>
    > <body>
    > <div id="bluebox" style="position :absolute; left:173px; top:48px;
    > width:100px; height:80px; z-index:1; background-color:#99CCFF;" ></div>
    > <div><a href="#" onClick="KP_gro wit('bluebox',4 00,300); return false;">grow
    > it</a></div>
    > </body>
    > </html>
    > ----------------------------------------------------------------------------
    > --------------------------------------------
    >
    >
    > I noticed that the script does not work because of the conditional
    > operators; however it DOES work when using the if...else format. Why is
    > this?[/color]
    Note the *: true* addition behind each ? ... statement. It *is* ? : and not
    ? by itself. My debugger picked up on that immediately.

    Fermin DCG

    Comment

    • TheKeith

      #3
      Re: Conditional shortcut (?:) limitations?


      "F. Da Costa" <dacosta@xs4all .nl> wrote in message
      news:401d70f2$0 $322$e4fe514c@n ews.xs4all.nl.. .[color=blue]
      > TheKeith wrote:
      >[color=green]
      > > I just wrote the following script for something I'm working on:
      > >
      > >[/color]
      >
      > --------------------------------------------------------------------------[/color]
      --[color=blue][color=green]
      > > -------------------
      > > <html>
      > > <head>
      > > <script type="text/javascript">
      > > function KP_growit(subje ct,widthto,heig htto) {
      > > var target = document.getEle mentById(subjec t);
      > > var tweenit = setInterval(gro wer,10);
      > >
      > > function grower() {
      > > (parseInt(targe t.style.width) < widthto)? target.style.wi dth =
      > > (parseInt(targe t.style.width) + 10) + "px";
      > > (parseInt(targe t.style.height) < heightto)? target.style.he ight =
      > > (parseInt(targe t.style.height) + 10) + "px";
      > > (parseInt(targe t.style.width) == widthto &&[/color][/color]
      parseInt(target .style.height)[color=blue][color=green]
      > > == heightto)? clearInterval(t weenit);
      > > }
      > > }[/color]
      > function grower() {
      > (parseInt(targe t.style.width) < widthto)? target.style.wi dth =
      > (parseInt(targe t.style.width) + 10) + "px": true;
      > (parseInt(targe t.style.height) < heightto)? target.style.he ight =
      > (parseInt(targe t.style.height) + 10) + "px" : true;
      > (parseInt(targe t.style.width) == widthto &&[/color]
      parseInt(target .style.height)[color=blue]
      > == heightto)? clearInterval(t weenit) : true;
      > }
      > }[color=green]
      > > </script>
      > > </head>
      > > <body>
      > > <div id="bluebox" style="position :absolute; left:173px; top:48px;
      > > width:100px; height:80px; z-index:1; background-color:#99CCFF;" ></div>
      > > <div><a href="#" onClick="KP_gro wit('bluebox',4 00,300); return[/color][/color]
      false;">grow[color=blue][color=green]
      > > it</a></div>
      > > </body>
      > > </html>[/color]
      >
      > --------------------------------------------------------------------------[/color]
      --[color=blue][color=green]
      > > --------------------------------------------
      > >
      > >
      > > I noticed that the script does not work because of the conditional
      > > operators; however it DOES work when using the if...else format. Why is
      > > this?[/color]
      > Note the *: true* addition behind each ? ... statement. It *is* ? : and[/color]
      not[color=blue]
      > ? by itself. My debugger picked up on that immediately.[/color]


      Much appreciated--but I'm still confused as to why that is required. If I
      can do conditionals in the if...else format without the else statement, why
      can't I do the same with ?: operator shortcut? Also, why does it have to be
      the word *true*--I tried it with just putting anything behind the *:*, like
      *: var hello = "hello"* for example, and that still causes an error--why is
      this?


      Comment

      • Michael Winter

        #4
        Re: Conditional shortcut (?:) limitations?

        On Sun, 1 Feb 2004 17:05:41 -0500, TheKeith <no@spam.com> wrote:

        [snip]
        [color=blue]
        > Much appreciated--but I'm still confused as to why that is required. If I
        > can do conditionals in the if...else format without the else statement,
        > why can't I do the same with ?: operator shortcut?[/color]

        It's not a shortcut; it's a ternary operator. Unary operators, like !,
        require one operand. Binary operators, like +, require two operands.
        Ternary operators, like ?:, require three. Only using two operands with
        them is like trying to use:

        var a = b *; // or a = * b;
        [color=blue]
        > Also, why does it have to be the word *true*--I tried it with just
        > putting anything behind the *:*, like *: var hello = "hello"* for
        > example, and that still causes an error--why is this?[/color]

        The syntax of the operator is:

        (expr) ? (if-true expr) : (if-false expr)

        The first expression is evaluated. If it is considered true, the second
        operand (if-true expr) is evaluated and returned. If the first expression
        is considered false, the third operand (if-false expr) is evaluated and
        returned.

        The main use for the operator is conditional assignment. Say, for example,
        you wanted to ensure that an argument in a function is always valid[1]:

        function a( x ) {
        x = (undefined == x) ? 0 : x;
        }

        a();

        Here, no argument was passed to function a(), so the expression (undefined
        == x) will evaluate to true. This means that the operator, as a whole,
        will evaluate to 0 and 0 will be assigned to x.

        a( 6 );

        Here, (undefined == x) will evaluate as false because x is 6. The
        operator, as a whole, will evaluate to 6, so 6 will be assigned to x.

        If you are just wanted to do perform something based on a certain
        condition, then just use an if statement. If you want an expression to
        evaluate to a different value based on a condition, then use the
        conditional operator.

        I suggest you take a look at a JavaScript reference. Try:



        Beware of URL wrap.

        Mike

        [1] This isn't a particularly good example, but it's late and I'm tired,
        so bear with me :)

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        • TheKeith

          #5
          Re: Conditional shortcut (?:) limitations?


          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opr2pvw7vo 5vklcq@news-text.blueyonder .co.uk...[color=blue]
          > On Sun, 1 Feb 2004 17:05:41 -0500, TheKeith <no@spam.com> wrote:
          >
          > [snip]
          >[color=green]
          > > Much appreciated--but I'm still confused as to why that is required. If[/color][/color]
          I[color=blue][color=green]
          > > can do conditionals in the if...else format without the else statement,
          > > why can't I do the same with ?: operator shortcut?[/color]
          >
          > It's not a shortcut; it's a ternary operator. Unary operators, like !,
          > require one operand. Binary operators, like +, require two operands.
          > Ternary operators, like ?:, require three. Only using two operands with
          > them is like trying to use:
          >
          > var a = b *; // or a = * b;
          >[color=green]
          > > Also, why does it have to be the word *true*--I tried it with just
          > > putting anything behind the *:*, like *: var hello = "hello"* for
          > > example, and that still causes an error--why is this?[/color]
          >
          > The syntax of the operator is:
          >
          > (expr) ? (if-true expr) : (if-false expr)
          >
          > The first expression is evaluated. If it is considered true, the second
          > operand (if-true expr) is evaluated and returned. If the first expression
          > is considered false, the third operand (if-false expr) is evaluated and
          > returned.
          >
          > The main use for the operator is conditional assignment. Say, for example,
          > you wanted to ensure that an argument in a function is always valid[1]:
          >
          > function a( x ) {
          > x = (undefined == x) ? 0 : x;
          > }
          >
          > a();
          >
          > Here, no argument was passed to function a(), so the expression (undefined
          > == x) will evaluate to true. This means that the operator, as a whole,
          > will evaluate to 0 and 0 will be assigned to x.
          >
          > a( 6 );
          >
          > Here, (undefined == x) will evaluate as false because x is 6. The
          > operator, as a whole, will evaluate to 6, so 6 will be assigned to x.
          >
          > If you are just wanted to do perform something based on a certain
          > condition, then just use an if statement. If you want an expression to
          > evaluate to a different value based on a condition, then use the
          > conditional operator.[/color]


          that cleared it up--thanks a lot.


          Comment

          Working...