skip to Main Content

I have two variables like below:

var a = "active"  //[two possible value active/inactive]
var b = "inactive" //[three possible values active/locked/disabled]
var outcome = ""

    if(a=="active" && b=="active") 
      outcome = "a";
    elif(a=="active" && b=="locked") 
      outcome = "b"
    elif(a=="active" && b=="disabled")
      outcome = "c"
    elif(a=="inactive" && b=="active")
      outcome = "d"
    elif(a=="inactive" && b=="disabled")
      outcome = "e"
    elif(a=="inactive" && b=="locked")
      outcome = "f"

What is the most efficient way of describing the possible outcomes other than using ifelse checking for different conditions in JS?Please suggest.

3

Answers


  1. You could make your logic more data-driven by using an object, for example:

    var outcomeMap = {
      active: {
        active: "a",
        locked: "b",
        disabled: "c"
      },
      inactive: {
        active: "d",
        locked: "e",
        disabled: "f",
      }
    };
    

    You can then set your outcome variable by accessing this object using a, and then accessing the nested object’s value, for example:

    var outcome = outcomeMap[a][b];
    

    Note that if a can be anything other than the values you have mentioned, it’s best to check that outcomeMap[a] isn’t undefined before accessing b. If your environment supports it, that can be done using optional chaining outcomeMap[a]?.[b];

    Alternatively, you could set up arrays with your possible combinations, and then loop through them to check if your combinations match. You can then index into your results (outcomes), based on your current index if you find a result, for example:

    function getOutcome(a, b) {
      const aVals = ['active', 'inactive'];
      const bVals = ['active', 'locked', 'disabled'];
      const outcomes = [['a', 'b', 'c'], ['d', 'e', 'f']];
    
      for(const [i, aVal] of aVals.entries()) {
        for(const [j, bVal] of bVals.entries()) {
          if (aVal == a && bVal == b) {
            return outcomes[i][j];
          }
        }
      }
      // return ''; if you want a default value of empty string
    }
    const outcome = getOutcome('inactive','locked');
    console.log(outcome);

    Note that neither of these approaches are more efficient than using an if-statement. However, they should be easier to scale if you get more possiblilities.

    Login or Signup to reply.
  2. A simple way would be to simply add the string values of a and b and then define the possible outcomes in an object. For simplicity, I have not included all options in your example:

    var get_outcome = {
        'activeactive': 'a',
        'activelocked': 'b',
        'activedisabled': 'c'
    }
    

    Then you can simply get your outcome with:

    var outcome = get_outcome[a+b]
    

    Although, if else statements are quite fast in JavaScript, this might be an option if you have many elif statements.

    Login or Signup to reply.
  3. If your variables have finite number of possible values, I would first suggest using enums to store those and make code more readable:

    const StatusA = {
        Active: "active",
        Inactive: "inactive"
    }
    
    const StatusB = {
        Active: "active",
        Locked: "locked",
        Disabled: "disabled"
    }
    
    var a = StatusA.Active;
    var b = StatusB.Disabled;
    

    You could then use string concatenation to transform your elseifs to a switch case:

    switch (a + b) {
        case StatusA.Active + StatusB.Active: 
            console.log('outcome A');
            break;
        case StatusA.Active + StatusB.Locked: 
            console.log('outcome B');
            break;
        case StatusA.Active + StatusB.Disabled: 
            console.log('outcome C');
            break;
        case StatusA.Inactive + StatusB.Active: 
            console.log('outcome D');
            break;
        case StatusA.Inactive + StatusB.Locked: 
            console.log('outcome E');
            break;
        case StatusA.Inactive + StatusB.Disabled: 
            console.log('outcome F');
            break;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search