-1

I have this condition:

con1 ?  (con2 || con3) ? "blue" : "white" : "green"

Is this the correct interpretation of that code?

if con1 = true , then "blue"
if con1 = false, then "white"
if con2 || con3 = true, then "green"
?
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Derick Kolln
  • 633
  • 7
  • 17
  • Are you trying to understand a statement you found somewhere or to create your own to achieve your own results? The answers will be very different in those cases. (If you're trying to achieve something in particular, please update to add a description of what you want.) – Scott Sauyet Nov 27 '16 at 01:55
  • thanks scott...i would like to post my code, but i always fail to paste a codeblock – Derick Kolln Nov 27 '16 at 02:00
  • http://stackoverflow.com/editing-help – Scott Sauyet Nov 27 '16 at 02:05
  • But it's still not clear to me whether you are trying to learn from code you found in the wild or whether this is code you're trying to write to do something you want to accomplish... and it's not quite working for you. – Scott Sauyet Nov 27 '16 at 02:07
  • If we accept that this is not homework and really is code that you wrote then why would you write code you don't understand and can't parse for yourself? Just write the if then else statement you do understand instead if this stuff that you don't understand. – jwpfox Nov 27 '16 at 10:56
  • @scott: i try both : learn and wanna do my own. for this one, i found a code online and i wanna change it on my way, can you maybe help? – Derick Kolln Nov 28 '16 at 19:48
  • @DerickKolln: Really, you still haven't explained what it is you're trying to accomplish, so it's difficult to help. – Scott Sauyet Nov 29 '16 at 04:26

2 Answers2

2

The JavaScript ternary operator works like this:

condtion ? return value if condition is true : return value if condition is false

What you have is a nested ternary operator in the "true" portion, so your code:

con1 ?  (con2 || con3) ? "blue" : "white" : "green"

Can be thought of as being grouped like this:

con1 ? [ (con2 || con3) ? "blue" : "white" ] : "green"

So, the nested part is evaluated first:

(con2 || con3) ? "blue" : "white"

If con2 or con3 are true, "blue" is returned If not, "white" is returned

And that "blue" or "white" is then placed where the nested ternary was in the overall expression:

con1 ?  "blue" or "white" : "green"

So now, if con1 is true, "blue" or "white" (whichever was returned from the nested ternary) is returned.

If not, "green" is returned.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • @AJ con1 using the includes method on an array to check, if the nodes are a part of it. con2 is the starting node and con3 is the end node. the code highlights start node and end node with a first color, the nodes between those 2 nodes with a second color and the rest of the nodes gets a thirds color....my goal is to give the start node and the end node a different color – Derick Kolln Nov 27 '16 at 01:32
1
if con1 is false then "green"
if con1 is true and con2 || con3 is true then "blue"
if con1 is true and con2 || con3 is false then "white"

Explanation:

Condition is equivalent to :

con1 ? ( (con2 || con3) ? "blue" : "white" ) : "green"

as it is like:

condition ? expr1 : expr2 

if condition is true it executes expr1 else expr2.

therefore if con1 is true it will evaluate: (con2 || con3) ? "blue" : "white" else return "green".

Updated after Scott's comment:

First the condition (con2 || con3) ? "blue" : "white" ) is evaluated and then based on value returned it evaluated con1 ? [value returned from con2 || con3) ? "blue" : "white"] : "green"

Ajay Narain Mathur
  • 5,326
  • 2
  • 20
  • 32
  • thanks AJ! i am using this for highlighting nodes by mouseclick. now my question is this: con2 and con3 always shares the same color, how can i rewrite the code using just 1 line to make them get different color? – Derick Kolln Nov 27 '16 at 01:21
  • `con1 ? ( ((con2 )? (con3) ? "blue" : "white" : "red")) : "green"` how about this? – Derick Kolln Nov 27 '16 at 01:24
  • @DerickKolln That last one is not syntactically correct. – Scott Marcus Nov 27 '16 at 01:26
  • @DerickKolln : what is the exact condition like what should be get based on values of con1, con2 and con3 ? – Ajay Narain Mathur Nov 27 '16 at 01:27
  • @DerickKolln If you are looking to set up a variety of colors, you can store the colors in an array and then substitute the color names in your expression with references to members of the array using indexes that can change based on some other criteria. – Scott Marcus Nov 27 '16 at 01:28
  • @Scott Marcus: i think that is storing colors in an array is not needed, because i use a big graph in d3.js...but maybe i am talking wrong things... – Derick Kolln Nov 27 '16 at 01:38
  • *therefore if con1 is true it will evaluate: (con2 || con3) ? "blue" : "white" else return "green".* That's actually not true. Because of the nesting, the con2 || con3 test will be carried out first, regardless of whether or not con1 is true. – Scott Marcus Dec 03 '16 at 13:18