Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

Where do you put parentheses in this Javascript statement:

JavaScript
var temp = da >= d3_svg_arcMax ? r0 ? "Z" : "M0" : r0 ? "M" : "M";


This structure is used in d3 library and it's very confusing for me.

Thank you

What I have tried:

I looked for the answer on the internet but I couldn't find anything helpful. I also tried to debug and use breakpoints to find which operator has the higher priority but it didn't help me neither.
Posted
Updated 10-Aug-16 5:51am
Comments
David_Wimbley 10-Aug-16 11:43am    
What makes you think it needs a parenthesis? What do you want in parenthesis?

The following code compiles and runs so i don't know what the issue is.

var da = "2";
var d3_svg_arcMax = "3";
var r0 = "3";

var temp = da >= d3_svg_arcMax ? r0 ? "Z" : "M0" : r0 ? "M" : "M";
console.log("temp: " + temp);
Saman With You 10-Aug-16 11:48am    
I can't trace it. I need to know what is the priority of the operations. Which variable goes into "temp" at the end and why?

1 solution

It is a little confusing. Here is the outer operation

var temp = da >= d3_svg_arcMax ? X : Y;


This means if da>=d3_svg_arcMax then temp is "X" otherwise it is "Y".

So looking back at the original "X" is

r0 ? "Z" : "M0"


This means if r0 is true then evaluate to "Z" otherwise evaluate to "M0"

And "Y" is

r0 ? "M" : "M"


which means if r0 is true then evaluate to "M" otherwise evaluate to "M" - this is actually pointless and Y could be placed with just "M", but that's beside the point.

Putting it together, if da>=d3_svg_arcMax and r0 is true then temp is "Z", but if r0 is false then temp is "M0". If da is not >=d3_svg_arcMax and r0 is true then temp is "M", but if r0 is false temp is "M"

To use parenthesis it would be like

var temp = (da >= d3_svg_arcMax ? (r0 ? "Z" : "M0") : (r0 ? "M" : "M"));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900