Friday 30 March 2018

javascript - check to see if exactly two out of three booleans are true?



I need to test to see if exactly two out of three booleans are true.



Something like this:




if((a && b && !c) || (a && !b && c) || (!a && b && c)){
//success
}


Is this the most direct way to go about this? Does anyone know of a shortcut / shorthand?


Answer



To check if exactly two are equal to true:



[a, b, c].filter(Boolean).length === 2;



References:




No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...