-1

I have an if/else statement that I need to refactor and clean up so that I can shorten the amount of code I am using. The variables are being checked to see if they are not equal to the string 'TD'. What would be the best course of action to rewrite the if/else statement and/or make it shorter?

if ((VS_TDO1 != "TD") && (VS_TDO2 != "TD") && (VS_TDO3 != "TD") && (VS_TDO4 != "TD") && (VS_TDO5 != "TD") && (VS_TDO6 != "TD") && (VS_TDO7 != "TD") && (VS_TDO8 != "TD") && (VS_TDO9 != "TD") && (VS_TD10 != "TD") && (VS_TD11 != "TD") && (VS_TD12 != "TD")) {
            OPT_TDTD = "NO";
        } else {
            OPT_TDTD = "YES";
        }
  • 1
    Not the `if` condition needs the tidying. It's the declaration of those twelve variables that should have been an array. The `if` statement then will benefit implicitly. – Bergi May 21 '20 at 16:10
  • 2
    _"...industry standard format"_ - There's no such thing as a "industry standard format" – Andreas May 21 '20 at 16:10

1 Answers1

0

You could collect all values in an array and check with Array#every.

var allOptions = [VS_TDO1, VS_TDO2, VS_TDO3, VS_TDO4, VS_TDO5, VS_TDO6, VS_TDO7, VS_TDO8, VS_TDO9, VS_TDO10, VS_TDO11],
    OPT_TDTD = allOptions.every(s => s !== "TD")
        ? "NO"
        : "YES";
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392