Everything Be True - Need Help

Tell us what’s happening:

What’s wrong with my code?

Your code so far


function truthCheck(collection, pre) {
  // Is everyone being true?
  var result;

  for (var i = 0; i < collection.length; i++) {
    if (collection[i].pre == ("" || NaN || undefined || null || 0)) {
      result = false;
    }
  }
  return true;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true

The first problem is in the following line in which you tried to access keys with dot notation and a variable. Try to console.log(collection[i].pre) and you’ll see nothing printed to the screen.

It should be replaced with collection[i][pre].

The second problem is with the IF statement in which you tried to test multiple values against collection[i][pre] as a group. That will not work.

You should test it against each value, which means that you’ll have to repeat writing collection[i][pre] multiple times.

Example …collection[i][pre] == "" || collection[i][pre] == false || collection[i][pre] == 0… etc.

That is too much writing, isn’t it?

There’s a quick way to convert the values into truthy and falsy values and then based on that you can filter the result and return the right string.

This doesn’t mean that your method is wrong. It only means that you will write a lot of lines in order to get to the solution.

Don’t hesitate to ask more questions.
I will be glad to help.

This site point article does a good job at explaining truethy falsey values in JavaScript

Thanks. What’s the other quick way to do this as you have mentioned?

I get the concept of truthy and falsy values. But how to compare them in my code?

To convert data into Booleans, you just need to add double !! before the variable.

This one is already written in the article shared by @Bouncey

Here is a quick example:

!!collection[i][pre] // all values will be converted into Booleans, if the value is string it becomes true, if it is null, it becomes false and so on.

Another way to do it is to use filter() Method… etc.

So basically what you’re saying is this?

var result;

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

I am sorry this is the right code:

  var result;

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

But its still not returning the right result.

Read very well what I said to you
this one: !!collection[i][pre] only converts the data into Boolean

But you need one last step.

Think about it.

Oh i get it. This works!

  var result;

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

Great that you got that beautiful green window of success appearing on your screen.

So,

Good Luck & Happy Coding.

1 Like

One last thing. Why couldn’t i access the property with dot notation?

Okay. What would that be?

Could you give an example where it wouldn’t be a variable?

I see. Since it is an argument so that’s why only bracket notation can be used right?

Because it contains a string

For example
The variable user
which is equal to "Tinky-Winky"

if dot nation is used:

collection[i].user

it’s going to be equal to:

collection[i]."Tinky-Winky" => which is wrong.

1 Like

Thanks a lot. You’ve been a great help. Could i contact you directly in the future when I need help? I was facing huge problems understanding recursion and I literally had to copy and paste stuff in a couple of challenges to pass.

What is it?

With the filter() Method?

I think it can be done with every method as well.

I got it.

It’s easy.

you just add one ! and omit the == false

And it will be equal to: IF NOT TRUE => return false

2 Likes