I'm coming up a little short in Everything Be True

Tell us what’s happening:
So, my spade is turned. I am trying to solve for all the test cases, but the variations I am doing in my code solve for one section of the code or the other. Below I’ve tried to accommodate this, but it only solves for one section of the code. I’m not QUITE sure why as I thought that by adding || I would be arguing for either condition (the assignment of pre as the value of collection or as a property of it).

I think my problem could be solved in a little shorter code.
I also question whether I need to assign pre to collection as a value. I think this could be something different but I’m not sure what that would be.
Do I need to use a for loop for this? I don’t think it would make a difference and I think I would have the same problem I have now. But I’m wondering about filter() as a possible solution. I can’t think of why this would work, but filter() has Boolean properties …

Help please.

Your code so far


function truthCheck(collection, pre) {

    if(collection = pre || collection == pre){
      return true
    } else {
      return false;
    }
  
}

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

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

In the first one you are assigning pre to collection, it doesn’t work like that in a if statement, you are deleting `collection’. Also, can that comparison ever be true? What is one, what is the other?

Not exactly, filter needs a callback function that returns a Boolean - it is useful if you need to remove elements from an array

Because truthCheck function must look at every item of the passed in array and check whether every item’s property is “truthy”, I think the every() method works best. This is my code, hope it’s short enough:

function truthCheck(collection, pre) {
  return collection.every(item => item[pre])
}

Your if condition is faulty: there is no need to compare between collection and pre (or even assign pre to collection). Therefore it doesn’t work.

I’ve worked with the faults indicated in my code and solved the challenge, but I want to make sure that I understand it correctly. Would someone please look at my comments and tell me whether I am explaining my solution with the correct understanding or not?

Thank you!


function truthCheck(collection, pre) {
  //create variable that will enter into collection array to check if the pre perimeter is true in every instance
  let el;
  //.every method allows this because using the "el" variable as a perimeter.  If a truthy value is assigned to pre being a property of "el", then .every will check for this and determine if it holds water or not
  return collection.every(el=> el[pre])
    
  
}

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

the variable el inside the every() method is the function parameter, completely unrelated to the el variable you have created inside the function. you don’t need to do let el, you are declaring a variable that you never use.

the every() method takes one element of the array at a time and call the callback function on it, and then do something with all the values returned, in this case evaluates them all and returns true if all the values returned from the callback are true.

1 Like