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

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_layer) hideIt(set_layer);

Now why - and I know I should know this - does this condition never hide
a layer?
Jul 23 '05 #1
7 975
On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston
<us****@mpreston.demon.co.uk> wrote:

[snip]
"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_layer) hideIt(set_layer);

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


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
Jul 23 '05 #2
Michael Winter wrote:
On Tue, 10 Aug 2004 16:01:56 +0100, Mark Preston
<us****@mpreston.demon.co.uk> wrote:

[snip]

Because of the operator precedence.[snip]

Boom! (Sound of shooting own foot)...

I knew it was simple and I should have realised it. I missed out the
extra brackets...
Jul 23 '05 #3
On Tue, 10 Aug 2004 16:01:56 +0100, in comp.lang.javascript Mark
Preston <us****@mpreston.demon.co.uk> wrote:
| 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_layer) hideIt(set_layer);
You have:
if( not layer==set_layer) hideIt(set_layer);

Remove the exclamation symbol from the statement.
| Now why - and I know I should know this - does this condition never hide
| a layer?


---------------------------------------------------------------
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

jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jul 23 '05 #4
In article <cf*******************@news.demon.co.uk>,
Mark Preston <us****@mpreston.demon.co.uk> wrote:
if (!layer==set_layer) hideIt(set_layer);


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_layer);

as:

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

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

Robert
Jul 23 '05 #5
Mark Preston wrote:
[snip]

My one-liner is:

if (!layer==set_layer) hideIt(set_layer);

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). 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...?
Jul 23 '05 #6
On Wed, 11 Aug 2004 11:31:21 +0100, in comp.lang.javascript Mark
Preston <us****@mpreston.demon.co.uk> wrote:
| Mark Preston wrote:
|
| > [snip]
| >
| > My one-liner is:
| >
| > if (!layer==set_layer) hideIt(set_layer);
| >
| > 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).
Hey!!! I resemble that remark :-)
Yes I *am* blonde :-)
| 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...?


---------------------------------------------------------------
jn****@yourpantsbigpond.net.au : Remove your pants to reply
---------------------------------------------------------------
Jul 23 '05 #7
JRS: In article <rc*****************************@news4.west.earthl ink.n
et>, dated Tue, 10 Aug 2004 18:28:51, seen in news:comp.lang.javascript,
Robert <rc*******@my-deja.com> posted :
I avoid:
if( ! (a==b || c==d) ).

I find working out the truth tables in my mind hard.


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.demon.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.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 #8

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

Similar topics

62
by: ROSY | last post by:
hello experts plz answer following questions::: thanks in advance. 1. Out of fgets() and gets() which function is safe to use and why? 2. What is a far pointer? where we use it? 3. What does the...
9
by: See_Rock_City | last post by:
Hello All, I've decided that this OOP thing is not just a fad. With that in mind, I'm desparately trying to get rid of my function-oriented design paradigm and switch to a more object-centric...
4
by: ECathell | last post by:
I had read an article at one time that suggested a pattern to get around deeply nested if..then..else hell... Can anyone point me in that direction? select case statements wont work for me in...
4
by: tradmusic.com | last post by:
Hi, Newbie question... We have an online catalogue running off a mysql database, which we can back up. When we back it up, it creates a file online called "database.sql" which, when opened...
7
by: Kay Schluehr | last post by:
I want to manipulate a deeply nested list in a generic way at a determined place. Given a sequence of constant objects a1, a2, ..., aN and a variable x. Now construct a list from them recursively:...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
15
by: Gary Peek | last post by:
Can anyone tell us the browsers/versions that exhibit errors when tables are nested too deeply? And how many levels of nesting produces errors? (not a tables vs CSS question)
16
by: Cybex | last post by:
Can you have one or the other type conditions for a do while loop? See the non-working code below... do { cout << "Are you a Member or Non-member? Enter 'M' or 'N': "; cin >charMstat;...
2
by: joeschnell | last post by:
I'd be embarrassed to tell anyone how long it took me to get this far, since about 8pm last night! I have been assigned some homework to code a program that produces output in a diamond shape on...
6
by: Matt | last post by:
I'm almost embarrassed to ask this because it seems like a complete newbie question, but here goes. How do I find out the size of an array of pointers that defines a 2D matrix called 'matrix0'...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.