Question - and deeply embarrassed...

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

    #1

    Question - and deeply embarrassed...

    I know... I should be able to figure this out. Its silly and its simple
    but its driving me crackers. Don't tell me I should leave it a while and
    come back to it - I know I should and it will probably be blatantly
    obvious when I do. But at the moment its driving me nuts...

    I have a pop-up, pop-down set of layers. They are turned on and off by
    menu links. No problem, except that clicking a link for a layer when the
    layer is already displayed switches it off. I'm trying to prevent that
    with a simple, one-line, conditional:-

    "layer" is the layer to switch on.
    "set_layer" is the currently displayed layer.
    "hideIt()" hides the specified layer.

    My one-liner is:

    if (!layer==set_la yer) hideIt(set_laye r);

    Now why - and I know I should know this - does this condition never hide
    a layer?
  • Michael Winter

    #2
    Re: Question - and deeply embarrassed...

    On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston
    <usenet@mpresto n.demon.co.uk> wrote:

    [snip]
    [color=blue]
    > "layer" is the layer to switch on.
    > "set_layer" is the currently displayed layer.
    > "hideIt()" hides the specified layer.
    >
    > My one-liner is:
    >
    > if (!layer==set_la yer) hideIt(set_laye r);
    >
    > Now why - and I know I should know this - does this condition never hide
    > a layer?[/color]

    Because of the operator precedence. The negation operator has higher
    precedence then the comparison operator. This means that the object
    reference in 'layer' is logically negated and becomes a boolean. If the
    reference is an object (and it probably is), '!layer' will evaluate to
    false. If 'set_later' is is an object (and that probably is, too), it will
    evaluate to true. True doesn't equal false, so hideIt() is never called
    when it should be.

    What you intended is either

    if(layer != set_layer) ...

    or

    // Force the reference comparison first
    if(!(layer == set_layer)) ...

    Good luck,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail

    Comment

    • Mark Preston

      #3
      Re: Question - and deeply embarrassed...

      Michael Winter wrote:
      [color=blue]
      > On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston
      > <usenet@mpresto n.demon.co.uk> wrote:
      >
      > [snip]
      >
      > Because of the operator precedence.[snip][/color]
      Boom! (Sound of shooting own foot)...

      I knew it was simple and I should have realised it. I missed out the
      extra brackets...

      Comment

      • Jeff North

        #4
        Re: Question - and deeply embarrassed...

        On Tue, 10 Aug 2004 16:01:56 +0100, in comp.lang.javas cript Mark
        Preston <usenet@mpresto n.demon.co.uk> wrote:
        [color=blue]
        >| I know... I should be able to figure this out. Its silly and its simple
        >| but its driving me crackers. Don't tell me I should leave it a while and
        >| come back to it - I know I should and it will probably be blatantly
        >| obvious when I do. But at the moment its driving me nuts...
        >|
        >| I have a pop-up, pop-down set of layers. They are turned on and off by
        >| menu links. No problem, except that clicking a link for a layer when the
        >| layer is already displayed switches it off. I'm trying to prevent that
        >| with a simple, one-line, conditional:-
        >|
        >| "layer" is the layer to switch on.
        >| "set_layer" is the currently displayed layer.
        >| "hideIt()" hides the specified layer.
        >|
        >| My one-liner is:
        >|
        >| if (!layer==set_la yer) hideIt(set_laye r);[/color]

        You have:
        if( not layer==set_laye r) hideIt(set_laye r);

        Remove the exclamation symbol from the statement.
        [color=blue]
        >| Now why - and I know I should know this - does this condition never hide
        >| a layer?[/color]

        ---------------------------------------------------------------
        All I can say to the fundamentalist christians is:
        BRING BACK THE LIONS BRING BACK THE LIONS
        BRING BACK THE LIONS BRING BACK THE LIONS
        BRING BACK THE LIONS BRING BACK THE LIONS

        jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
        ---------------------------------------------------------------

        Comment

        • Robert

          #5
          Re: Question - and deeply embarrassed...

          In article <cfanna$58l$1$8 30fa17d@news.de mon.co.uk>,
          Mark Preston <usenet@mpresto n.demon.co.uk> wrote:
          [color=blue]
          > if (!layer==set_la yer) hideIt(set_laye r);[/color]

          I tend to only use the not equal operator (!=).

          I avoid:
          if( ! (a==b || c==d) ).

          I find working out the truth tables in my mind hard. I'd code:
          if (!( a==b || c==d)) hideIt(set_laye r);

          as:

          if ( a==b || c==d )
          {;}
          else
          { hideIt(set_laye r); }

          It is more to type, but for me, it is easier to read.

          Robert

          Comment

          • Mark Preston

            #6
            Re: Question - and deeply embarrassed...

            Mark Preston wrote:
            [color=blue]
            > [snip]
            >
            > My one-liner is:
            >
            > if (!layer==set_la yer) hideIt(set_laye r);
            >
            > Now why - and I know I should know this - does this condition never hide
            > a layer?[/color]

            Thanks for all the help folks - its nice to know there is someone out
            there when you have one of the inevitable "blonde moments" (as we call
            them here in the UK). Wonder why it is we poor coders can spend eight
            hours a day, five days a week doing this stuff and then one day just
            forget how to do one particular line...?

            Comment

            • Jeff North

              #7
              Re: Question - and deeply embarrassed...

              On Wed, 11 Aug 2004 11:31:21 +0100, in comp.lang.javas cript Mark
              Preston <usenet@mpresto n.demon.co.uk> wrote:
              [color=blue]
              >| Mark Preston wrote:
              >|
              >| > [snip]
              >| >
              >| > My one-liner is:
              >| >
              >| > if (!layer==set_la yer) hideIt(set_laye r);
              >| >
              >| > Now why - and I know I should know this - does this condition never hide
              >| > a layer?
              >|
              >| Thanks for all the help folks - its nice to know there is someone out
              >| there when you have one of the inevitable "blonde moments" (as we call
              >| them here in the UK).[/color]

              Hey!!! I resemble that remark :-)
              Yes I *am* blonde :-)
              [color=blue]
              >| Wonder why it is we poor coders can spend eight
              >| hours a day, five days a week doing this stuff and then one day just
              >| forget how to do one particular line...?[/color]

              ---------------------------------------------------------------
              jnorth@yourpant sbigpond.net.au : Remove your pants to reply
              ---------------------------------------------------------------

              Comment

              • Dr John Stockton

                #8
                Re: Question - and deeply embarrassed...

                JRS: In article <rccharles-4DAF9D.14344710 082004@news4.we st.earthlink.n
                et>, dated Tue, 10 Aug 2004 18:28:51, seen in news:comp.lang. javascript,
                Robert <rccharles@my-deja.com> posted :[color=blue]
                >I avoid:
                > if( ! (a==b || c==d) ).
                >
                >I find working out the truth tables in my mind hard.[/color]

                Just LEARN - not (A or B) = (not A) and (not B)
                not (A and B) = (not A) or (not B)

                See <URL:http://www.merlyn.demo n.co.uk/js-logic.htm>.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> JL / RC : FAQ for 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

                Working...