Is there any way to use boolean algebra in JS?
Eg I would like to loop through an array containing true & false, and simplify it down to either only true, or false.
Doing it with boolean algebra seems like an elegant way to do it...
[true,true,true,true] //would like to do a comparison that lets me
//simply add the previous value to the current iteration of a loop
// and have this return true
[false,true,true,true]//this on the other hand should return false
I think a simple solution would be
return array.indexOf(false) == -1
Try Array.reduce
:
[false,true,true,true].reduce(function(a,b) { return a && b; }) // false
[true,true,true,true].reduce(function(a,b) { return a && b; }) // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With