Skip to main content
Inspiring
February 13, 2016
Answered

Shortening if / else string

  • February 13, 2016
  • 1 reply
  • 685 views

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;

}

This topic has been closed for replies.
Correct answer try67

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 {

    // ???

}

1 reply

Inspiring
February 13, 2016

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.

try67
Community Expert
Community Expert
February 13, 2016

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 {

     /// ???

}

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 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 {

    // ???

}