Kalle - 8 months ago 44

Javascript Question

I have 4 variables, any of them can be true or false

`var a`

var b

var c

var d

I would like to test 3 conditions:

- true if 1 out of 4 is true
- true if 2 out of 4 is true
- true if 3 out of 4 is true

I know that 0 out of 4 is true when:

`a!=true && b!=true && c!=true && d!=true`

and

4 out of 4 are true when:

`a==true && b==true && c==true && d==true`

but i cant think further.

Answer

You could just add the variables with boolean values. The result is a number.

```
value = a + b + c + d;
```

Then you could make a check agains the number, you want to test.

```
var a = false, b = true, c = true, d = true,
value = a + b + c + d;
console.log(value);
```

Source (Stackoverflow)