How to translate this ternary operator expression into if/else blocks?How to force the browser to reload cached CSS/JS files?How do you access the matched groups in a JavaScript regular expression?How do you use a variable in a regular expression?Ternary operation in CoffeeScriptIs Safari on iOS 6 caching $.ajax results?JS ternary operator vs if/elseTurning a ternary operator into if else statement on returnConvert ternary operator statement to normal if/elseif/else statementif else-works,if else with ternary operator doesn't workOnly ternary operator works here

How to drill holes in 3/8" steel plates?

Is that a case of "DOUBLE-NEGATIVES" as claimed by Grammarly?

What the real concept of Static keyword in perspective of Embedded C. See below code

When I press the space bar it deletes the letters in front of it

How can I effectively communicate to recruiters that a phone call is not possible?

Is it OK to leave real names & info visible in business card portfolio?

Reverse dots and boxes

How do native German speakers usually express skepticism (using even) about a premise?

Misrepresented my work history

Integer Lists of Noah

Backspace functionality in normal mode

What is the correct parsing of お高くとまる?

Party going through airport security at separate times?

Postgres trigram match acting strange for specific characters

How can a dictatorship government be beneficial to a dictator in a post-scarcity society?

Data Encryption by Application vs Data Encryption in Database

Yet another hash table in C

What are the original Russian words for a prostitute?

Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?

How quality assurance engineers test calculations?

[Future]Historical experience as a guide to warship design?

What's the point of having a RAID 1 configuration over incremental backups to a secondary drive?

Elf (adjective) vs. Elvish vs. Elven

Graduate student with abysmal English writing skills, how to help



How to translate this ternary operator expression into if/else blocks?


How to force the browser to reload cached CSS/JS files?How do you access the matched groups in a JavaScript regular expression?How do you use a variable in a regular expression?Ternary operation in CoffeeScriptIs Safari on iOS 6 caching $.ajax results?JS ternary operator vs if/elseTurning a ternary operator into if else statement on returnConvert ternary operator statement to normal if/elseif/else statementif else-works,if else with ternary operator doesn't workOnly ternary operator works here






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am having a hard time to "translate" this expression using JS ternary operators into if/else statement, which help me visualise better what is happening in the code.




mouseX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);









share|improve this question






















  • you use if (condition) mouseX = when true else when false as opposed to mouseX = condition ? when true : when false - note, when false will be a second if (condtion) mouseX = when true else mouseX = when false

    – Jaromanda X
    Mar 26 at 0:39


















0















I am having a hard time to "translate" this expression using JS ternary operators into if/else statement, which help me visualise better what is happening in the code.




mouseX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);









share|improve this question






















  • you use if (condition) mouseX = when true else when false as opposed to mouseX = condition ? when true : when false - note, when false will be a second if (condtion) mouseX = when true else mouseX = when false

    – Jaromanda X
    Mar 26 at 0:39














0












0








0








I am having a hard time to "translate" this expression using JS ternary operators into if/else statement, which help me visualise better what is happening in the code.




mouseX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);









share|improve this question














I am having a hard time to "translate" this expression using JS ternary operators into if/else statement, which help me visualise better what is happening in the code.




mouseX = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 0:37









Leonardo PetroliLeonardo Petroli

1




1












  • you use if (condition) mouseX = when true else when false as opposed to mouseX = condition ? when true : when false - note, when false will be a second if (condtion) mouseX = when true else mouseX = when false

    – Jaromanda X
    Mar 26 at 0:39


















  • you use if (condition) mouseX = when true else when false as opposed to mouseX = condition ? when true : when false - note, when false will be a second if (condtion) mouseX = when true else mouseX = when false

    – Jaromanda X
    Mar 26 at 0:39

















you use if (condition) mouseX = when true else when false as opposed to mouseX = condition ? when true : when false - note, when false will be a second if (condtion) mouseX = when true else mouseX = when false

– Jaromanda X
Mar 26 at 0:39






you use if (condition) mouseX = when true else when false as opposed to mouseX = condition ? when true : when false - note, when false will be a second if (condtion) mouseX = when true else mouseX = when false

– Jaromanda X
Mar 26 at 0:39













3 Answers
3






active

oldest

votes


















2














We can try writing:



if (window.Event) 
mouseX = e.pageX;

else
var scroll;
if (document.documentElement.scrollLeft)
scroll = document.documentElement.scrollLeft;

else
scroll = document.body.scrollLeft;

mouseX = event.clientX + scroll;



I pulled out the nested ternary expression into a variable scroll. This makes it much easier to rewrite the logic.






share|improve this answer






























    2














    It'll be clearer if you indent your original code - changing only whitespace, your original code is:



    mouseX = (window.Event)
    ? e.pageX
    : event.clientX + (
    document.documentElement.scrollLeft
    ? document.documentElement.scrollLeft
    : document.body.scrollLeft
    );


    Translating that, you get:



    if (window.Event)
    mouseX = e.pageX;
    else
    let add;
    if (document.documentElement.scrollLeft)
    add = document.documentElement.scrollLeft;
    else
    add = document.body.scrollLeft;
    mouseX = event.clientX + add;






    share|improve this answer
































      0














      First of all I want to suggest you to use some linting (eslint, jslint, tslint etc) tool and prettier. To have better code formatting. It helps in long run.



      The other answers are also valid. But I want to write one.



      if (window.Event) 
      mouseX = e.pageX;
      else
      let scroll;
      if (document.documentElement.scrollLeft)
      scroll = document.documentElement.scrollLeft;
      else
      scroll = document.body.scrollLeft;

      mouseX = event.clientX + scroll;






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348289%2fhow-to-translate-this-ternary-operator-expression-into-if-else-blocks%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        We can try writing:



        if (window.Event) 
        mouseX = e.pageX;

        else
        var scroll;
        if (document.documentElement.scrollLeft)
        scroll = document.documentElement.scrollLeft;

        else
        scroll = document.body.scrollLeft;

        mouseX = event.clientX + scroll;



        I pulled out the nested ternary expression into a variable scroll. This makes it much easier to rewrite the logic.






        share|improve this answer



























          2














          We can try writing:



          if (window.Event) 
          mouseX = e.pageX;

          else
          var scroll;
          if (document.documentElement.scrollLeft)
          scroll = document.documentElement.scrollLeft;

          else
          scroll = document.body.scrollLeft;

          mouseX = event.clientX + scroll;



          I pulled out the nested ternary expression into a variable scroll. This makes it much easier to rewrite the logic.






          share|improve this answer

























            2












            2








            2







            We can try writing:



            if (window.Event) 
            mouseX = e.pageX;

            else
            var scroll;
            if (document.documentElement.scrollLeft)
            scroll = document.documentElement.scrollLeft;

            else
            scroll = document.body.scrollLeft;

            mouseX = event.clientX + scroll;



            I pulled out the nested ternary expression into a variable scroll. This makes it much easier to rewrite the logic.






            share|improve this answer













            We can try writing:



            if (window.Event) 
            mouseX = e.pageX;

            else
            var scroll;
            if (document.documentElement.scrollLeft)
            scroll = document.documentElement.scrollLeft;

            else
            scroll = document.body.scrollLeft;

            mouseX = event.clientX + scroll;



            I pulled out the nested ternary expression into a variable scroll. This makes it much easier to rewrite the logic.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 26 at 0:41









            Tim BiegeleisenTim Biegeleisen

            258k13 gold badges109 silver badges170 bronze badges




            258k13 gold badges109 silver badges170 bronze badges























                2














                It'll be clearer if you indent your original code - changing only whitespace, your original code is:



                mouseX = (window.Event)
                ? e.pageX
                : event.clientX + (
                document.documentElement.scrollLeft
                ? document.documentElement.scrollLeft
                : document.body.scrollLeft
                );


                Translating that, you get:



                if (window.Event)
                mouseX = e.pageX;
                else
                let add;
                if (document.documentElement.scrollLeft)
                add = document.documentElement.scrollLeft;
                else
                add = document.body.scrollLeft;
                mouseX = event.clientX + add;






                share|improve this answer





























                  2














                  It'll be clearer if you indent your original code - changing only whitespace, your original code is:



                  mouseX = (window.Event)
                  ? e.pageX
                  : event.clientX + (
                  document.documentElement.scrollLeft
                  ? document.documentElement.scrollLeft
                  : document.body.scrollLeft
                  );


                  Translating that, you get:



                  if (window.Event)
                  mouseX = e.pageX;
                  else
                  let add;
                  if (document.documentElement.scrollLeft)
                  add = document.documentElement.scrollLeft;
                  else
                  add = document.body.scrollLeft;
                  mouseX = event.clientX + add;






                  share|improve this answer



























                    2












                    2








                    2







                    It'll be clearer if you indent your original code - changing only whitespace, your original code is:



                    mouseX = (window.Event)
                    ? e.pageX
                    : event.clientX + (
                    document.documentElement.scrollLeft
                    ? document.documentElement.scrollLeft
                    : document.body.scrollLeft
                    );


                    Translating that, you get:



                    if (window.Event)
                    mouseX = e.pageX;
                    else
                    let add;
                    if (document.documentElement.scrollLeft)
                    add = document.documentElement.scrollLeft;
                    else
                    add = document.body.scrollLeft;
                    mouseX = event.clientX + add;






                    share|improve this answer















                    It'll be clearer if you indent your original code - changing only whitespace, your original code is:



                    mouseX = (window.Event)
                    ? e.pageX
                    : event.clientX + (
                    document.documentElement.scrollLeft
                    ? document.documentElement.scrollLeft
                    : document.body.scrollLeft
                    );


                    Translating that, you get:



                    if (window.Event)
                    mouseX = e.pageX;
                    else
                    let add;
                    if (document.documentElement.scrollLeft)
                    add = document.documentElement.scrollLeft;
                    else
                    add = document.body.scrollLeft;
                    mouseX = event.clientX + add;







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 26 at 0:43

























                    answered Mar 26 at 0:40









                    CertainPerformanceCertainPerformance

                    117k16 gold badges78 silver badges107 bronze badges




                    117k16 gold badges78 silver badges107 bronze badges





















                        0














                        First of all I want to suggest you to use some linting (eslint, jslint, tslint etc) tool and prettier. To have better code formatting. It helps in long run.



                        The other answers are also valid. But I want to write one.



                        if (window.Event) 
                        mouseX = e.pageX;
                        else
                        let scroll;
                        if (document.documentElement.scrollLeft)
                        scroll = document.documentElement.scrollLeft;
                        else
                        scroll = document.body.scrollLeft;

                        mouseX = event.clientX + scroll;






                        share|improve this answer



























                          0














                          First of all I want to suggest you to use some linting (eslint, jslint, tslint etc) tool and prettier. To have better code formatting. It helps in long run.



                          The other answers are also valid. But I want to write one.



                          if (window.Event) 
                          mouseX = e.pageX;
                          else
                          let scroll;
                          if (document.documentElement.scrollLeft)
                          scroll = document.documentElement.scrollLeft;
                          else
                          scroll = document.body.scrollLeft;

                          mouseX = event.clientX + scroll;






                          share|improve this answer

























                            0












                            0








                            0







                            First of all I want to suggest you to use some linting (eslint, jslint, tslint etc) tool and prettier. To have better code formatting. It helps in long run.



                            The other answers are also valid. But I want to write one.



                            if (window.Event) 
                            mouseX = e.pageX;
                            else
                            let scroll;
                            if (document.documentElement.scrollLeft)
                            scroll = document.documentElement.scrollLeft;
                            else
                            scroll = document.body.scrollLeft;

                            mouseX = event.clientX + scroll;






                            share|improve this answer













                            First of all I want to suggest you to use some linting (eslint, jslint, tslint etc) tool and prettier. To have better code formatting. It helps in long run.



                            The other answers are also valid. But I want to write one.



                            if (window.Event) 
                            mouseX = e.pageX;
                            else
                            let scroll;
                            if (document.documentElement.scrollLeft)
                            scroll = document.documentElement.scrollLeft;
                            else
                            scroll = document.body.scrollLeft;

                            mouseX = event.clientX + scroll;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 26 at 1:23









                            thakurinboxthakurinbox

                            18110 bronze badges




                            18110 bronze badges



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348289%2fhow-to-translate-this-ternary-operator-expression-into-if-else-blocks%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript