Everything be true challenge, test logic question

Everything be true challenge, test logic question
0

#1

Hello. I’ve read the (many) threads on this challenge, and I have a solution, but I still do not understand why the ‘!’ negator works, but the affirmative condition does not? For example, this condition works as a test (partial code below, to not give away answer)

    if(!collection[i][pre])
      return false;
  }
  return true;

But this code does not work.

    if(collection[i][pre])
    return true;
  }
  return false;

I noticed that in the second version above, the ‘test’ does not seem to get applied past the first object in the argument ‘collection’, but I would like to understand why this is. Thanks for your help!


#2

The problem with the second example returns true on the first pass through the (I assume) for loop, thus evaluating the entire exercise as true.

The logic goes, if ANY of them return falsy, then the entire collection is flagged falsy. By returning the first time you get a false result, you’re efficiently checking the truthiness of that field in every member.

If, instead, you return true at the first true result, then the rest of the collection is never evaluated. Remember, a return statement breaks you out of any loops, as well as breaking you out of your function itself.


#3

Thank you snowmonkey! I understand now:smiley: