lundi 9 septembre 2019

Setting variable across multiple if/then statements

I am using JS to set a total of 7 variables based on conditional statements and looking for the most efficient way to do this.

Currently, there are 16 different conditions that are really grouped into 3 different categories if you will with each category containing multiple conditions but one specific condition that remains the same within the category. In addition, there is one variable that must be consistent across all conditions

Here's an example:

if      (myvar === "A" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "A" && var1 === 2 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "A" && var1 === 3 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "B" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "B" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "B" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }

Is there a better way to do this? I'd like to only have to set the condition of var2 once and have it apply to all the conditional statements. It would also but better if I could set the value of myvar one time for the first three statements and one time for the last 3.

if      (myvar === "A" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "A" && var1 === 2 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "A" && var1 === 3 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "B" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "B" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }
else if (myvar === "B" && var1 === 1 && var2 ==="1")
    {
        setVar = "Value";
    }

Aucun commentaire:

Enregistrer un commentaire