5 Replies Latest reply on Feb 13, 2016 10:19 AM by Duquette

    Shortening if / else string

    Duquette

      I am wondering if there is anyway I can integrate the information for an If / Else statement in one central location instead of having to write out everything. My form takes a trailer gross weight and subtracts the weight of the empty trailer. What I have works, but is bulky and takes up a lot of memory. Here is a partial example (add 35 more trailers and 13 form entries):

       

      else if (this.getField("QTrailerF").value == '1R793')

      {

        if (heelf>0) event.value = heelf-13360;

      }

      else if (this.getField("QTrailerF").value == '1r793')

      {

        if (heelf>0) event.value = heelf-13360;

      }

      else if (this.getField("QTrailerF").value == '8058')

      {

        if (heelf>0) event.value = heelf-22480;

      }

      else if (this.getField("QTrailerF").value == '1R504')

      {

        if (heelf>0) event.value = heelf-13200;

      }

      else if (this.getField("QTrailerF").value == '1r504')

      {

        if (heelf>0) event.value = heelf-13200;

      }

      else if (this.getField("QTrailerF").value == '1R553')

      {

        if (heelf>0) event.value = heelf-13300;

      }

      else if (this.getField("QTrailerF").value == '1r553')

      {

        if (heelf>0) event.value = heelf-13300;

      }

      else if (this.getField("QTrailerF").value == '4180')

      {

        if (heelf>0) event.value = heelf-13090;

      }

        • 1. Re: Shortening if / else string
          gkaiseril MVP & Adobe Community Professional

          What are the possible values for the "QTrailerF" field?

           

          Is capitalization of the "QTraqilerF" field important? if not why not just convert the value to lower case and drop what appears to be possible duplicate test.

           

          Using a drop down list to select the value for the "QTrailerF" field would make sure only the desired values are used.

           

          I would also look at using the "switch" since multiple matches can be grouped to run only one block of code. Also once a match is made and the block of code is run the rest of the switch statement is ignored.

           

          If this code is used several times I would look at using a document level function to use the two tested values as parameters and return the resulting values which then can be used as needed like setting a field value.

          • 2. Re: Shortening if / else string
            try67 MVP & Adobe Community Professional

            Since all of the conditions are dependent on heelf being positive, you can extract it, like this:

             

            if (heelf>0) {
                if (this.getField("QTrailerF").value == '1R793') {
                  event.value = heelf-13360;
                } else if (this.getField("QTrailerF").value == '1r793') {
                  event.value = heelf-13360;
                }
                // etc.
            } else {
                 /// ???
            }
            
            • 3. Re: Shortening if / else string
              try67 MVP & Adobe Community Professional

              Another thing you can do to make the code shorter is to do a case-insensitive comparison, like this (instead of the two conditions above:

               

              if (heelf>0) {
                  if (this.getField("QTrailerF").value.toUpperCase() == '1R793') { // this covers both 1R793 and 1r793
                    event.value = heelf-13360;
                  } // etc.
              } else {
                  // ???
              }
              
              • 4. Re: Shortening if / else string
                Duquette Level 1

                The "QTrailerF" field represents a trailer number in the fleet - there are approximately 30 different values.

                 

                Dropping the lower case tests is a good idea - Thanks.

                 

                Drop down menu's would limit the entries to only those listed - I need the flexibility to be able to add info if needed - 16 people use this form in the field.

                 

                I will look into a "switch" ...... LOL have no idea what it is.

                 

                I don't know if I can set a specific field value - the information changes for every job - there can be as few as one trailer or in the job I am currently working 12 trailers.

                • 5. Re: Shortening if / else string
                  Duquette Level 1

                  Thank You - that will cut approximately 1/3 off the code.