Everything Be True Not working

Tell us what’s happening:

It doesn’t pass two tests(You can try the code and see yourself)

Your code so far


function truthCheck(collection, pre) {
  for(var unknown of collection) {
  var x = new Boolean(unknown[pre] === true);
  }
  return x.valueOf();
}

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 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

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

You will have to re-think your logic.

unknown[pre] === true

You don’t want to check if values are literally true, just that they are truthy. Read the challenge description again for more information.

Truthy values are all values except falsy values which are converted into false.

So anything else is true if got converted to boolean(by using Boolean object)

I don’t see what’s wrong.

unknown[pre] === true

This does not check if a value is truthy, it checks whether the value is the boolean true.

var booleanTrue = true; // the literal value true
var truthyTrue = 'a truthy string'; // truthy, but not true. 

truthyTrue === true; // false
Boolean(truthyTrue) === true; // true
1 Like

Oh, I understand the problem now.

However, I could solve this challenge in 2 minutes easily early but the problem is that it will requires long and ineffective code(which I try to prevent in all my challenges/projects), at least for me.

Pro-tip: Always start with the code that works. You can make changes later.

You put the parentheses in the wrong place

# says, return a boolean from unknown[pre] === true
var x = new Boolean(unknown[pre] === true);

# says, return a boolean from unknown[pre], and then check if `=== true`
var x = new Boolean(unknown[pre]) === true;

Well, I gave up on the code I made earlier so I solved the problem with the longest and ineffective code, sigh ):

var arr = [];
for(var unknown of collection) {
arr.push(!!unknown[pre]);
}
if(arr.includes(false)) {return false}
return true;
}

Sadly it still doesn’t work.

It wasn’t going to work as written, because I was just giving you a hint.

Since you solved it, look at the spoiler where I explain how your original logic was very close to working. You just made some small logical mistakes.

I find it best to go line by line, just how the js engine would read the code

First, like I mentioned before, you weren’t converting a value to a boolean

var x = new Boolean(unknown[pre] === true);

That line says,

  • “check if unkown[pre] exists and matches true. Then create a boolean from that”

So you are converting the result of checking if the value is truthy.

What I like to do is replace the values of the check to verify my logic, so let’s do that.

/**
* prop exists
* new Boolean(falsey) since since 'string' is not equal to true
*/
var x = new Boolean('female' === true) 

/**
* prop doesn't exist
* new Boolean(falsey) since since 'string' is not equal to true
*/
var x = new Boolean('' === true);

Your intention was right. The only thing is that you wanted to

1. "check if unknown[pre] exists and convert that result to a boolean
2. extract the value from the boolean object before checking if it’s true
3. then check if it matches true

/**
* prop exists
* true since new Boolean(truthy) === true
*/
var x = new Boolean('female').valueOf() === true 

/**
* prop doesn't exist
* false since new Boolean(falsey) === true
*/
var x = new Boolean('').valueOf() === true 

OK, so now we know how to properly convert the checks to boolean.

Now the requirement asks you to return true or false.

Whenever you see returns that could only be a specific set of values, you can always default to the one that they all can share.

In this case, since we want to make sure all objects have a property, we can default to true because if even one object fails, the whole test fails.

So we move the default value outside the loop, and only change it when the test fails.

function truthCheck(collection, pre) {
// default value
  var x = true
  for (var unknown of collection) {
// here we only update the default if the test fails
    if(new Boolean(unknown[pre]).valueOf() !== true) x = false
  }
  return x;
}

Hopefully that clears it up for you. Let me know if there’s something I didn’t make clear.

Hello @OGTechnoBoy, I want to help you with this. Understanding the probelm is important and if we’re clear with what we need we can get the solution.

We have to check the Boolean value of the “pre” property’s value of the object, Boolean[Object[pre]], to know more about Boolean function check with the link

In my code, I have iterated loop through the array and picked out the false cases, and breaked the loop when I found one first false case and stored false in a varible named “result”.

By default, I have stored the value true in “result” variable.

Finally, I have returned the variable “result”

To check Boolean value of the “pre” property’s value of all the objects present in the array(remember “collection” variable in the exercise is an array collection of objects), we can write a simple code like this

function truthCheck(collection, pre) {
// Is everyone being true?
let result = true;
for(let i=0; i<collection.length; i++){
if(!Boolean((collection[i])[pre])){
result = false;
break;
}
}
return result;
}
truthCheck([{“user”: “Tinky-Winky”, “sex”: “male”}, {“user”: “Dipsy”, “sex”: “male”}, {“user”: “Laa-Laa”, “sex”: “female”}, {“user”: “Po”, “sex”: “female”}], “sex”);