Skip to main content
deleted 9 characters in body
Source Link
Conor O'Brien
  • 40.2k
  • 3
  • 91
  • 180
!a.includes(e)
!~a.indexOf(e)
a.every(t=>t!=e)
!a.filter(t=>t==e).length==a.length
for(i=0,c=1;i<a.length;c*=e!=a[i++]);
!a.includes(e)
!~a.indexOf(e)
a.every(t=>t!=e)
a.filter(t=>t==e).length==a.length
for(i=0,c=1;i<a.length;c*=e!=a[i++]);
!a.includes(e)
!~a.indexOf(e)
a.every(t=>t!=e)
!a.filter(t=>t==e).length
for(i=0,c=1;i<a.length;c*=e!=a[i++]);
added 737 characters in body
Source Link
Conor O'Brien
  • 40.2k
  • 3
  • 91
  • 180
Traditional conjuction: s&&t
Equivalent conjuction: s*t OR s&t

Traditional disjunction: s||t
Equivalent disjunction: s+t OR s|t

No members of a satisfy condition p: !a.some(p).

Element e exists in array a:

a.includes(e)                        // 13 bytes, standard built-in
~a.indexOf(e)                        // 13 bytes, "traditional" method
a.find(x=>e==x)                      // 15 bytes, find (ES6)
a.some(x=>x==e)                      // 15 bytes, some (ES5)
(a+"").search(e)                     // 16 bytes, buggy
a.filter(t=>t==e).length             // 24 bytes, no reason to use this
for(i=c=0;i<a.length;c+=e==a[i++]);  // 35 bytes, super-traditional

Element e does not exist in array a:

!a.includes(e)
!~a.indexOf(e)
a.every(t=>t!=e)
a.filter(t=>t==e).length==a.length
for(i=0,c=1;i<a.length;c*=e!=a[i++]);
Traditional conjuction: s&&t
Equivalent conjuction: s*t

Traditional disjunction: s||t
Equivalent disjunction: s+t

No members of a satisfy condition p: !a.some(p).

Traditional conjuction: s&&t
Equivalent conjuction: s*t OR s&t

Traditional disjunction: s||t
Equivalent disjunction: s+t OR s|t

No members of a satisfy condition p: !a.some(p).

Element e exists in array a:

a.includes(e)                        // 13 bytes, standard built-in
~a.indexOf(e)                        // 13 bytes, "traditional" method
a.find(x=>e==x)                      // 15 bytes, find (ES6)
a.some(x=>x==e)                      // 15 bytes, some (ES5)
(a+"").search(e)                     // 16 bytes, buggy
a.filter(t=>t==e).length             // 24 bytes, no reason to use this
for(i=c=0;i<a.length;c+=e==a[i++]);  // 35 bytes, super-traditional

Element e does not exist in array a:

!a.includes(e)
!~a.indexOf(e)
a.every(t=>t!=e)
a.filter(t=>t==e).length==a.length
for(i=0,c=1;i<a.length;c*=e!=a[i++]);
Source Link
Conor O'Brien
  • 40.2k
  • 3
  • 91
  • 180

Golfing Logical Operations in ES6

"GLOE (S6)"

General Logic

Say you have constructed statements s and t. See if you can use any of the following replacements:

Traditional conjuction: s&&t
Equivalent conjuction: s*t

Traditional disjunction: s||t
Equivalent disjunction: s+t

(These may not work if the order is wrong; i.e. + and * have a lower order precedence than || and && do.)

Also, here are some handy logical expressions:

  • Either s or t is true/XOR: s^t
  • s and t are the same truth value: !s^t or s==t

Array logic

All members of a satisfy condition p:

a.every(p)                             // 10 bytes (11 bytes saved)
a.map(x=>c&=p(x),c=1)                  // 21 bytes (16 bytes saved)
for(i=0,c=1;i<a.length;c&=p(a[i++]));  // 37 bytes (hideously long)

At least one member of a satisfies condition p:

a.some(p)                            // 9  bytes (13 bytes saved)
a.map(x=>c|=p(x),c=0)                // 21 bytes (14 bytes saved)
for(i=c=0;i<a.length;c|=p(a[i++]));  // 35 bytes (just please no)

No members of a satisfy condition p: !a.some(p).