2

This one stumped me for a while, and I eventually just looked at the solution online. The hint was fairly cryptic, and manipulating "De Morgan's Laws" in JS with &&, !, || is pretty complicating still. I'm starting to grasp it, but not quite there yet. I imagine I will need a fair bit of practice and time to understand these concepts thoroughly. Anyway, here is the question for one of the exercises at the end of chapter 5:

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.

function every(array, test) {
  // Your code here.
}

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

I was able to get the first version using array.every; it wasn't too hard. But the solution for using the some method I still don't understand. Here's the solution to the second part of the question:

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

I'm trying to work this out in my head, and thought that maybe a SO member could help me understand what's happening. Can someone talk me through this?

Thanks in advance!

Jordan

2
  • 3
    every is the same as a negated some with a negated condition. Reason through it verbally: if x is true for every item y, that just means that there is not some item y where x is false. Easy to see the relationship with a concrete example: "Every sparrow is a bird" --> "There is not some sparrow which is not a bird". Another way: "Every item passes my test" --> "There is not some item which fails my test". And so on... Commented Jun 25, 2020 at 17:31
  • @CRice -- this is an excellent answer. Thank you! I took a break after JS'ing this morning and drove around and smoked a cig and my brain was sort of starting to grasp what you are saying.. I was thinking-- "it's almost like it's just a way for some() to get through all the elements in the array instead of returning on the first true. So it's basically just an every(). And then here you are saying it. I love the example phrases as well. Thanks again! Commented Jun 25, 2020 at 18:34

2 Answers 2

4

You could build a table of truth and have a look which value yields which result.

This example take the second example and shows for the first two elements an intermediate result, pretending here would stop the loop and the final result.

            n < 10               !(n < 10)
  value    compare1     every     compare2    some       !some    comment
---------  ---------  ---------  ---------  ---------  ---------  -------------------
     2        true                 false 
     4        true                 false

                         true                 false       true    intermediate result

    16       false                  true

                        false                  true      false    final result
                        ^^^^^                            ^^^^^

By mentioning De Morgan's laws, a term can be expressed by changing the operator from logical AND && to logical OR || or vice versa by taking negated values and finally negate the whole expression.

a && b && c
!(!a || !b || !c)

Both expressions are equal (for boolean values).

Array#every acts like using && and Array#some like ||.

To negate some, take every and vice versa. This replacement need to negate the condition inside of the callback and a negation of the the result.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent answer Nina, thank you so much. I can tell you understand this concept deeply, a lot like the author of EloquentJS. I want to build this table myself and so I can understand it better. I think as I understand it more, your answer will become more and more informative for me. I will start with building the table today or tomorrow and filling in my own values -- and then I will come back to the logical AND vs logical OR explanations a little further down the line. Thanks again!
0

I am currently solving this task. According to the condition for option 1, the Array.every() method could not be used. You need to make a custom function that will work as an Array.every(). Here are my solutions: I will be very grateful for your advice and guidance on mistakes.

let numbers1 = [3, 4, 9, 7];
let numbers2 = [3, 4, 1, 7];

function every2(array, predicat) {
    if (array.constructor !== Array) return "not array";
    let result = Boolean;
    for (let i = 0; i < array.length; i++) {
       result = predicat(i);
       if (!result) break;
    }
    return result;
}

console.log(every2(numbers1, (i => numbers1[i] > 2)));  //true
console.log(every2(numbers2, (i => numbers2[i] > 2)));  //false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.