Why is the first one incorrect and the second one correct? (Everything Be True)

So I tried this first but it didn’t work.


function truthCheck(collection, pre) {
for (var i = 0; i < collection.length; i++) {
    if (collection[i][pre] != true) {
      return false;
  }
}
return true;
}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

Then found this solution which does work.


function truthCheck(collection, pre) {
for (var i = 0; i < collection.length; i++) {
    if (!collection[i][pre]) {
      return false;
  }
}
return true;
}

What is the difference between (collection[i][pre] != true) and (!collection[i][pre])?

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36.

Challenge: Everything Be True

Link to the challenge:

Hey @ryandcorrigan ,

The ! operator is called the Logical NOT operator, which flips a value from a truthy into a falsy and vice versa.
More about Logical NOT (!) here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT


Anyways, did you see what I used there, truthy and falsy. In JavaScript truthy means ANY value except for any falsy values, which I will talk about.
Here’s more about truthy:

https://developer.mozilla.org/en-US/docs/Glossary/Truthy


Talking about false, falsy is the similar to how truthy works, as it represents a value that is false. This also apply if a value is empty or 0. So, undefined, null, 0, '' is considered falsy.
Here’s more about falsy:

https://developer.mozilla.org/en-US/docs/Glossary/Falsy


Knowing more about truthy and falsy and how the ! works now, let’s get back to your question:

  • Your code below checks if the value is not true, which means ANYTHING that is not true (not truthy, which means a string will be considered not true), will pass this statement, and it will return false
  • While this code, the ! operator makes it so that any FALSY values passes this, so anything that is NOT FALSY, which is everything but 0, null, undefined, etc… will not pass the statement and will return true.

Hope this helps you understand more about ! (NOT operator) and how truthy and falsy works :slight_smile:

1 Like

Sorry, there are a couple things I don’t quite grasp.

  1. You said: “(not truthy, which means a string will be considered not true)”
    But aren’t strings TRUTHY (unless they are empty)?

  2. Re: the incorrect code
    “Anything that is NOT TRUE will pass this statement and RETURN FALSE”
    Re: the correct code
    “the ! operator makes it so that any FALSY values passes (…the statement and RETURN FALSE)”

I kinda had to paraphrase but it sounded like you were saying the same thing just in different ways.
NOT TRUE > WILL PASS > RETURN FALSE
FALSY > WILL PASS > RETURN FALSE

Or is NOT TRUE != FALSY?

Yes, I said not true not not truthy a string is a truthy but it’s not literally true

What I meant by this is that the if statement will not pass and will skip itself.
By NOT TRUE, I meant literally.
Anything except for true and the number 1 will pass the if statement and run everything inside the if statement

  • So the ! operator will make the value of whatever is after it a falsy if it’s a truth and vice versa.
  • An if statement check the statement inside the parentheses is truthy, if there isn’t then it just skips. So let’s see the code again:
if (!collection[i][pre]) 

Let’s say this is passed into the statement:

 {"user": "Dipsy"}

and the predicate is sex.

  • Since the property sex doesn’t exist in this property, collection[i][pre] will return undefined.
  • Since undefined is a falsy value, the ! then say, OOOH this value is a falsy, Let me switch it into a truthy
  • Then the if statement goes in and check, is the statement inside the parentheses is truthy, and it does.
  • So the if statement goes and runs everything inside the {}brackets

Let’s say we pass this into the statement

{"user": "Laa-Laa", "sex": "female"}

and the predicate is also sex

  • Since the property sex exist, and it does have a value, the collection[i][pre] will return the value
  • Since any value except for falsy is truthy, the ! comes in and say, OOOH this value is truthy, let me switch it into a falsy.
  • Then the if statement goes in and check, is the statement inside the parentheses is truthy, in this case it doesn’t
  • So the if statement will skip itself and won’t run anything inside the {}brackets

So to sum it all up,

  • != true > CHECKS IF THE VALUE IS NOT TRUE (not truthy, literally true) Which is any value in except for 1 and true(itself). > WILL PASS > RETURN FALSE

  • !value > CHECKS IF THE VALUE IS FALSY, then it turns the falsy value into a truthy and vice versa so that the if statement will run it > DEPENDS ON VALUE IT WILL PASS > RETURN FALSE

  • != true != !value

Hope this helps to clarify a bit.
If you still have questions, please ask again, I would be happy to help you.

I want to help you on this until you understand this :slight_smile:

1 Like

Okay that sorta makes sense now. I’m going to come back to this in the morning and read it again. I’m too tired for it to fully register atm lol.

Unfortunately I kept conflating true with truthy and false with falsy.
But it is really good that I asked about this so I don’t keep confusing “!=” with “!value”.

Thanks for all your help. I can tell you put lots of effort into explaining it for me.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.