A tech blog....from a guy named Eddie

some() from every()

One of the early challenges at Hack Reactor was to build the some() function for arrays by utilizing the every() function. This problem had held up a lot of people, including myself, but it’s actually quite simple.

The long explanation

Let’s say we have an isOdd() iterator function that returns true if the value is odd and false otherwise. Here’s the truth table given the arrays [1,1], [1,2], and [2,2]:

array isOdd() some() every()
[1,1] [T,T] T T
[1,2] [T,F] T F
[2,2] [F,F] F F

It doesn’t look like much, but let’s invert the isOdd() iterator and see what happens with every():

array isOdd() some() every() !isOdd() every()
[1,1] [T,T] T T [F,F] F
[1,2] [T,F] T F [F,T] F
[2,2] [F,F] F F [T,T] T

This looks like the some() function, but inverted. So now we invert every():

array isOdd() some() every() !isOdd() every() !every()
[1,1] [T,T] T T [F,F] F T
[1,2] [T,F] T F [F,T] F T
[2,2] [F,F] F F [T,T] T F

And that’s it. We were able achieve the same results as the some() function just by inverting the iterator and every(). All that’s left is to build the function:

var some = function(array, iterator) {
  return !every(array, function(item) {
    return !iterator(item);

The short explanation

For anyone who’s ever seen a truth table, the every() and some() functions should look familiar:

some() every()

These tables are exactly the same as the “and” and “or” truth tables, respectively, which makes sense - in a two element array, every() is true only when both element1 and element2 pass a truth test, while some() is true when either element1 or element2 pass the test. Let’s trace this with some logical pseudocode:

if( isOdd(element1) || isOdd(element2) )


if( isOdd(element1) && isOdd(element2) )

If we negate every(), we’ll get something that kinda resembles some():

if( !(isOdd(element1) && isOdd(element2)) )
if( !isOdd(element1) || !isOdd(element2) )

Here, along with negating the operator (&& becomes || - equivalent to every() becoming !every()), we’re also negating the results of the isOdd function calls. This is exactly the same result as the long answer above.