Skip to content

Instantly share code, notes, and snippets.

@jonurry
Created February 20, 2018 10:57
Show Gist options
  • Select an option

  • Save jonurry/15015c95c2065fea9c2b0c4b744826e9 to your computer and use it in GitHub Desktop.

Select an option

Save jonurry/15015c95c2065fea9c2b0c4b744826e9 to your computer and use it in GitHub Desktop.
5.3 Everything (Eloquent JavaScript Solutions)
// 5.3 Everything
//every using a loop
function every(array, test) {
for (let element of array) {
if (test(element) === false) {
return false;
}
}
return true;
}
console.log(every([1, 3, 5], n => n < 10));
// → true
console.log(every([2, 4, 16], n => n < 10));
// → false
console.log(every([], n => n < 10));
// → true
// every using array.some
function every(array, test) {
return array.some(test);
}
console.log(every([1, 3, 5], n => n < 10));
// → true
console.log(every([2, 4, 16], n => n < 10));
// → false
console.log(every([], n => n < 10));
// → true
@jonurry
Copy link
Author

jonurry commented Feb 20, 2018

5.3 Everything

Analogous to the some method, arrays also have an every method. This one returns true when the given function returns true for every element in the array. In a way, some is a version of the || operator that acts on arrays, and every is like the && operator.

Implement every as a function that takes an array and a predicate function as parameters. Write two versions, one using a loop and one using the some method.

@jonurry
Copy link
Author

jonurry commented Feb 20, 2018

Hints

Like the && operator, the every method can stop evaluating further elements as soon as it has found one that doesn’t match. So the loop-based version can jump out of the loop—with break or return—as soon as it runs into an element for which the predicate function returns false. If the loop runs to its end without finding such an element, we know that all elements matched and we should return true.

To build every on top of some, we can apply “De Morgan’s laws”, which state that a && b equals !(!a || !b). This can be generalized to arrays, where all elements in the array match if there is no element in the array that does not match.

@jimflores5
Copy link

Jon,
Creating a for loop to check the array elements against the test condition was fairly straightforward:
for_loop

However, I struggled quite a bit when trying to make the function work using the .some method.  After working for 30+ minutes, I looked at the hints - to no avail.  I then cut and pasted your solution into the editor, but that still did not work:

using_some

What am I missing?  If I understand correctly, array.some(test) returns true if ANY element in the array passes the test condition (hence [2, 4, 16] evaluates as true even though 16 > 10).  Similarly, !array.some(test) will return true if NONE of the elements pass the test.  How do I solve the case where SOME (pardon the pun) of the elements pass the test, but others do not?

Thank you,
Jim

@cmatschiner
Copy link

Hi Jim,

just merely applying the some() method will not work, since latter returns true when one element in the array passes the test.

To test if ALL elements pass the test with the some() method, use De Morgan’s laws, which states that
!(A && B) === !A || !B

We can modify De Morgan’s laws by using a double negation:
(A && B) === ! (!A || !B)

Here the solution:

function every(array, test) {
    return !array.some(element => !test(element));
}

@hamzachenguiti
Copy link

hamzachenguiti commented Sep 26, 2022

Hi Jim,

just merely applying the some() method will not work, since latter returns true when one element in the array passes the test.

To test if ALL elements pass the test with the some() method, use De Morgan’s laws, which states that !(A && B) === !A || !B

We can modify De Morgan’s laws by using a double negation: (A && B) === ! (!A || !B)

Here the solution:

function every(array, test) {
    return !array.some(element => !test(element));
}

I used the some method with for/of loop, but now I understand how to use some to confirm that all the items of the list passed the test with the help of De Morgan's law.
Thanks, Christoph!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment