ding
ding

Reputation: 49

simplify JS conditions into if else

I am trying to simplify this conditions into just if((A && B && C) || (!A && !B && !C)){..} else {..}.

Could you help me with this?

Upvotes: 0

Views: 176

Answers (5)

Nina Scholz
Nina Scholz

Reputation: 386604

You could change the first condition to check if all variables have the same value, then delete, otherwise take and array with corresponding letters.

function check(a, b, c) {
    if (a === b && b === c) {
        console.log(a, b, c, 'delete options.state');
    } else {
        console.log(a, b, c, `this.set('controller.options.state', '${['A', 'B', 'C'].filter((_, i) => [a, b, c][i]).join()}');`);
    }
}

check(false, false, false);
check(false, false, true);
check(false, true, false);
check(false, true, true);
check(true, false, false);
check(true, false, true);
check(true, true, false);
check(true, true, true);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

VLAZ
VLAZ

Reputation: 29003

(A && B && C) || (!A && !B && !C)

is equivalent to

A === B && B === C

since either A, B and C are all true or they are all false. In either case, they are equal. If A equals B and B equals C then transitively A also equals C. Alternatively this can be expressed as A === B && A === C - using the same logic but transitively C is equal to B.

However, if you wish to simplify the whole if/else chain, you can easily encode it in a table and do a lookup instead of if/elsees. This can help if there are different things that can happen in different conditions as you can more easily encode and find what is where:

//create the initial table
const table = {
  true : { true: {}, false: {}, },
  false: { true: {}, false: {}, },
}

const shared = ({options}) => delete options.state;
//fill in the table with handlers
//    |  A  ||  B  ||  C  |
table [true ][true ][true ] = shared;
table [false][false][false] = shared;
table [true ][true ][false] = ({self}) => self.set(`controller.options.state`, 'A,B');
table [false][true ][true ] = ({self}) => self.set(`controller.options.state`, 'A,C');
table [true ][false][false] = ({self}) => self.set(`controller.options.state`, 'A');
table [false][true ][false] = ({self}) => self.set(`controller.options.state`, 'B');
table [false][false][true ] = ({self}) => self.set(`controller.options.state`, 'C');

/* ... */
//get handler or use a default no-op
const handler = table[A][B][C] ?? () => {};
handler({options, self: this});

In this case, all handlers take an object with a self and a options property, so they can use it as needed.

Upvotes: 0

Aadit M Shah
Aadit M Shah

Reputation: 74204

I would do something like this.

switch (4 * A + 2 * B + C) {
case 0:
    console.log("None");
    break;
case 1:
    console.log("C");
    break;
case 2:
    console.log("B");
    break;
case 3:
    console.log("B,C");
    break;
case 4:
    console.log("A");
    break;
case 5:
    console.log("A,C");
    break;
case 6:
    console.log("A,B");
    break;
case 7:
    console.log("A,B,C");
    break;
}

See the demo.

for (const A of [false, true]) {
    for (const B of [false, true]) {
        for (const C of [false, true]) {
            console.log(test(A, B, C));
        }
    }
}

function test(A, B, C) {
    switch (4 * A + 2 * B + C) {
    case 0:
        return "None";
    case 1:
        return "C";
    case 2:
        return "B";
    case 3:
        return "B,C";
    case 4:
        return "A";
    case 5:
        return "A,C";
    case 6:
        return "A,B";
    case 7:
        return "A,B,C";
    }
}

Upvotes: 1

piercus
piercus

Reputation: 1256

const conds = {A,B,C};
const keys = Objects.keys(conds);
if(keys.every(k => conds[k]) || keys.every(k => !conds[k])){
  delete options.state;
} else {
  this.set(`controller.options.state`, keys.filter(k => conds[k]).join(','));
}

Upvotes: 1

Bergi
Bergi

Reputation: 664548

You might be looking for

const vals = [];
if (A) vals.push('A');
if (B) vals.push('B');
if (C) vals.push('C');
if (vals.length > 0 && vals.length < 3)
  this.set(`controller.options.state`, vals.join(','));
else
  delete options.state;

Upvotes: 1

Related Questions