-
-
Save jonurry/15015c95c2065fea9c2b0c4b744826e9 to your computer and use it in GitHub Desktop.
| // 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 |
Jon,
Creating a for loop to check the array elements against the test condition was fairly straightforward:

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:
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
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));
}
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!

Hints
Like the
&&operator, theeverymethod 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—withbreakorreturn—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
everyon top ofsome, we can apply “De Morgan’s laws”, which state thata && bequals!(!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.