Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Conditional (Ternary) Operator (?:)

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anybody explain me the foll lines :
display = display?display !row.style.display?"none" row.style.display == "inline"?"none":"inline"));
row.style.display = display;
img.src = display == "inline"?"'images/minus.gif'":"'images/plus.gif'";

Thanks,
Shriya

[edit - disbaled smiles]
[ July 15, 2008: Message edited by: Eric Pascarello ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
break it up into if else

<blockquote>code:
<pre name="code" class="core">

if(display){}
else if( !row.style.display ){ display = "none" }
else if( row.style.display == "inline"){ display = "none" }
else display = "inline";

</pre>
</blockquote>

Does that make sense?

All it is doing is checking to see if a row is visible or not and flips it to the opposite value.

Eric
 
Viidhya Kishore
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Eric.
It was very helpful.

Y cant they just write simple if statements
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer

<blockquote>code:
<pre name="code" class="core">
var foo = variable1 || variable2;
</pre>
</blockquote>



Eric
 
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shriya Kishore:
Y cant they just write simple if statements


Please use real words when posting to the forums. Abbreviations such as "y" in place of "why" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
 
Bear Bibeault
Sheriff
Posts: 67650
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shriya Kishore:
Y cant they just write simple if statements

The ternary operator can be very useful and terse when making a simple decision in an expression. It can also be confusing when used as shown in your example. A judicious mix of if statements and the ternary operator can be used to achieve code that is as brief as possible, while still being readable and clear.

A good dose of thoughtful code formatting can also go a long way to enhancing the clarity of the code.
reply
    Bookmark Topic Watch Topic
  • New Topic