• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Shortening if / else string

Explorer ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

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;

}

TOPICS
Acrobat SDK and JavaScript , Windows

Views

418

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 13, 2016 Feb 13, 2016

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 {

    // ???

}

Votes

Translate

Translate
LEGEND ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

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 {

     /// ???

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

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 {

    // ???

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines