Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean algebra in javascript

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
like image 748
Alex Avatar asked Nov 27 '22 22:11

Alex


2 Answers

I think a simple solution would be

return array.indexOf(false) == -1
like image 52
Lourens Avatar answered Nov 29 '22 13:11

Lourens


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
like image 42
digitalbath Avatar answered Nov 29 '22 13:11

digitalbath