• Products
    • Isadora
    • Get It
    • ADD-ONS
    • IzzyCast
    • Get It
  • Forum
  • Help
  • Werkstatt
  • Newsletter
  • Impressum
  • Dsgvo
  • Press
  • Isadora
  • Get It
  • ADD-ONS
  • IzzyCast
  • Get It
  • Press
  • Dsgvo
  • Impressum

Navigation

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags

    Javascript "or" operator [solved]

    Troubleshooting and Bug Reports
    javascript
    3
    6
    2355
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Maxime
      Maxime last edited by Maxime

      Hello,


      I want to do a javascript actor which has a string input (the name of a sequence) and return an integer depending on the input. As many sequences name will output the same number, i wanted to use the "or" operator to keep my code as short as possible like:

      function main()
      {
          var step = arguments[0]
          if(step == '3'|| '4'|| '5'|| '6A'|| '6B'|| '6C'|| '7A'|| '7B'|| '8A'|| '8B'){
              return 1;    
          }else if(step == '11B'|| '11C'){
              return 2;
          }else{
              return 100;
          }
      }
       

      But it works only for the first test of the first statement (here "3" ) and then is unable to test anything else, even in the next if statement...

      Is there some mistake in my code or is it a Isadora limitation?

      I've got it working around by using a if statement for each possibility, so it's more a cosmetic problem and general knowledge... :)

      Anybody knows about it? (made in Isadora 2.6.1)

      MBP 15" end 2013 / i7 2,3Ghz /NVIDIA GT 750M 2048Mo / 16Go RAM / OS 10.10.3
      Razer Blade 15" 2018 /i7/ GTX1070/ 16Go RAM

      1 Reply Last reply Reply Quote 0
      • Juriaan
        Juriaan Tech Staff last edited by

        Personally I would write something like this :

        function main()
        {
            var step = arguments[0];
            
            switch(step) {
                
                case "3" || "4" || "5" | "6A" || "6B" || "6C" || "7A" || "7B" || "8A" || "8B":
                    return 1;
                case "11B" || "11C":
                    return 2;
                default:
                    return 100;
            }
        }


        This is called a Switch statement in JavaScript and meant for complex cases where we have to do multiple long if statements. It will work it's way through the stack, until it gets a break (or a return in this case, since a return also breaks the loop)

        Isadora 3.1.1, Dell XPS 17 9710, Windows 10
        Interactive Performance Designer, Freelance Artist, Scenographer, Lighting Designer, TroikaTronix Community moderator
        Always in for chatting about interaction in space / performance design. Drop me an email at hello@juriaan.me

        1 Reply Last reply Reply Quote 0
        • Juriaan
          Juriaan Tech Staff last edited by

          Regarding your OP question :

          Take this line of code :

          step = "3";
          if (step == "1" || "2"){

          return "This should not execute !";

          }

          else {

          return "Yeah !";

          }

          The above code snippet wil return "This should not execute !", you expect that the code will be skipped since it is false right ? That is not the case ! step == "1" will return false, since our string is "3", but "2" will return true. Why is this ? Because we aren't saying is step == "2", we are saying is "2" equal to true ? And the answer in JavaScript to that question is yes (True)


          If True / False are combined it will return the last positive value. So in this case the code will stop by "2", causing the return "This should not execute !" to be executed.

          Isadora 3.1.1, Dell XPS 17 9710, Windows 10
          Interactive Performance Designer, Freelance Artist, Scenographer, Lighting Designer, TroikaTronix Community moderator
          Always in for chatting about interaction in space / performance design. Drop me an email at hello@juriaan.me

          1 Reply Last reply Reply Quote 0
          • Maxime
            Maxime last edited by

            thanks @Juriaan for the detailed answer.

            I didn't think of using a switch, but it's true it's much smoother than what I did.

            Nonetheless, I still have the same problem than before, it will work with the first values of each case ( "3" and "11B" in the example code) but the following values will return the default value.

            So the "or" operator is not working as far as I understand

            (thanks for the 2nd explanation also, I realise my mistake now)

            MBP 15" end 2013 / i7 2,3Ghz /NVIDIA GT 750M 2048Mo / 16Go RAM / OS 10.10.3
            Razer Blade 15" 2018 /i7/ GTX1070/ 16Go RAM

            DusX 1 Reply Last reply Reply Quote 0
            • DusX
              DusX Tech Staff @Maxime last edited by DusX

              @maxime

              try breaking your logic up...
              eg: if (step == "1" || step == "2")

              see for more options: https://stackoverflow.com/ques... (ignore the jQuery stuff, and note EcmaScript 6 'mostly, as supported by Googles' V8 engine' is what is supported in Isadora 2.6.1)

              Troikatronix Technical Support

              • New Support Ticket Link: https://support.troikatronix.com/support/tickets/new
              • My Add-ons: https://troikatronix.com/add-ons/?u=dusx
              • Profession Services: https://support.troikatronix.com/support/solutions/articles/13000109444-professional-services

              Running: Win 11 64bit, i7, M.2 PCIe SSD's, 32gb DDR4, nVidia GTX 4070 | located in Ontario Canada.

              1 Reply Last reply Reply Quote 1
              • Maxime
                Maxime last edited by Maxime

                thanks @DusX 

                I indeed did a mistake in my first attempt with if statement and then jump to Juriaan solution with the switch without spending much time on my first attempt.

                So the "or" operator works properly in the "if" statement when properly repeating the condition each time (well explained by Juriaan in his second comment)

                While the switch case like in @Juriaan first comment doesn't work because switch doesn't take "or" operator ( as explained here ) , instead, you must do something called a "fall-through":

                function main()
                {
                var step = arguments[0];
                switch(step){
                    case 'seq1A':
                    case 'seq1B':
                    case 'seq1C':
                        return 1;
                    case 'seq2A':
                    case 'seq2B':
                        return 2;
                    default:
                        return 100;
                }
                }

                I've got it mixed up by trying several solutions in parallel without understanding the specificities of each one, but it's all working now. 

                Thanks for your help, I've learned something today! :)

                MBP 15" end 2013 / i7 2,3Ghz /NVIDIA GT 750M 2048Mo / 16Go RAM / OS 10.10.3
                Razer Blade 15" 2018 /i7/ GTX1070/ 16Go RAM

                1 Reply Last reply Reply Quote 2
                • First post
                  Last post