Everything Be True - Need Help

Everything Be True - Need Help
0

#1

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


#2

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.


#3

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


#4

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


#5

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


#6

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.


#7

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;

#8

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.


#9

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.


#10

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;

#11

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

So,

Good Luck & Happy Coding.


#12

There is a much simpler way to write the above.


#13

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


#14

Okay. What would that be?


#15

Because pre is a variable and you must use bracket notation with a variable for a property name.

Dot notation can only be used if part after the dot is an actual property name. There are no properties named “pre” in the objects.


#16

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


#17

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


#18

Yes, you are correct.


#19

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.


#20

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.