Help with Everything Be True

I do not understand what is the reason why these tests are failing.
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex") should return false.

truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age") should return false.

truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastForward", "onBoat": null}], "onBoat") should return false
truthCheck([{"single": "double"}, {"single": undefined}], "single") should return false

truthCheck([{"single": "double"}, {"single": NaN}], "single") should return false

function truthCheck(collection, pre) {

                      

           for(let e of collection){

                 

                 if(e[pre]==""||e[pre]==null||e[pre]==NaN||e[pre]==undefined){

                         

                         return false

                 }else{

                         return true

                 }  

                          

           }

}

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

/*

Check if the predicate (second argument) is truthy on all elements of a collection (first argument).

In other words, you are given an array collection of objects. The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false.

In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.

Remember, you can access object properties through either dot notation or [] notation.*/

Hello~!

The problem is your use of return statements.

Remember that return ends a function. So you aren’t checking every element in collection, just the first one, and returning true or false depending on that one check. :slight_smile:

1 Like

Hello!!! Thanks for your answer. I was not seeing that big mistake :0 I will solve it!

It is not working yet. I never get true :frowning:

function truthCheck(collection, pre) {

     let a=[]

         for(let e in collection){

                 

                 if(collection.hasOwnProperty(pre)){

                               a.push(e)               

                 }

         }

     if(a.length==collection.length){

             return true

     }else{

             return false

     }

}

truthCheck([{"single": ""}, {"single": "double"}], "single");

Okay. It looks like the problem you have is here:

Currently, this is checking if the collection has a property. That will never return true, as collection is an array. You can check if a specific element in collection has a property by using bracket notation - you started setting that up with let e in collection, but you’re not accessing e you’re accessing the whole collection array.

Once you fix that, you’ll see that everything becomes true because you’re only checking if the property is there, but not if the property has a value. You’ll need to include that in your if statement too.

So you know, I was able to get your code to work by only changing one line:
if(collection.hasOwnProperty(pre))
So don’t worry about changing any other code and just focus on what this one if statement is looking for. :slight_smile:

2 Likes

That formatting makes your code hard to read.

function truthCheck(collection, pre) {
  let a=[];

  for (let e in collection) {
    if (collection.hasOwnProperty(pre)) {
      a.push(e);
    }
  }

  if (a.length == collection.length) {
    return true;
  } else {
    return false;
  }
}

console.log(truthCheck([{"single": ""}, {"single": "double"}], "single"));

Side Note: You want to use === instead of == except in extremely rare circumstances.

Other Side Note: I’d just keep a count of e that meet the criteria instead of making a full copy of collection. Only store what you need - which in this case is just a count.

1 Like
collection[e].hasOwnProperty(pre)?

I will use ternary one in some moments.

You will want to

Check if the predicate (second argument) is truthy on all elements of a collection (first argument).

Are you checking the truthiness of the property on an element of the collection with that if statement?

Re “ternary one”: Do you mean ===?

A ternary is something else entirely (condition ? resultIfTrue : resultIfFalse).

=== is called a ‘strict comparison operator’. Always use === unless you really really need to use == (which is almost absolutely never).

1 Like

About this snippet:

if (a.length == collection.length) {
  return true;
} else {
  return false;
}

It would be much simpler to just return a.length === collection.length; btw.

1 Like

Thanks for your correction. I confused the words.

I will change it. Thanks!

What may I do to check is a value is thruthy?

function truthCheck(collection, pre) {

    

      //console.log(collection,pre)

let a=[];

//console.log(collection[0].hasOwnProperty(pre))

      for(let i=0;i<collection.length;i++){

             //    console.log(i)

          if(collection[i].hasOwnProperty(pre)  && (collection[i][pre]!=undefined ||collection[i][pre]!=""||collection[i][pre]!=null)){

                    

           a.push(collection[i])             

          }

                                                                     

      }           

console.log(a)

    

}

truthCheck([{"single": "double"}, {"single": undefined}], "single");

From the lesson description:

In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.

That is to say,

if (isThisTruthy) {
  return "Yes, that's truthy";
} else {
  return "No, that's falsey";
}
1 Like

I am thankful whith everyone.

function truthCheck(collection, pre) {

  let a=[];        

          

          console.log(collection[0][pre], pre)         

  

  for(let i=0;i<collection.length;i++){

              

         if(collection[i][pre]){

                a.push(collection[i])              

             } 

                 

               }

     console.log(a)              

      if(collection.length===a.length){

                 return true;

      }else{

                return false;

      }

}

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

Another way.

function truthCheck(collection, pre) {

  let a=[];        

          

          console.log(collection[0][pre], pre)         

  

  for(let i=0;i<collection.length;i++){

              

         if(collection[i][pre]){

                a.push(collection[i])              

             } 

                 

               }

     console.log(a)              

     return collection.length===a.length

}