# freeCodeCamp Challenge Guide: Everything Be True

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 8:28pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011 "2016-06-30T20:28:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 8:28pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/1 "2016-06-30T20:28:14Z")

</div>

# [Everything Be True](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true)

* * *

## Problem Explanation

The program needs to check if the second argument is a [truthy](http://forum.freecodecamp.com/t/javascript-truthy-value/15975) element, and it must check this for each object in the first argument.

#### Relevant Links

- [JavaScript Truthy](http://forum.freecodecamp.com/t/javascript-truthy-value/15975)
- [JavaScript Arguments](http://forum.freecodecamp.com/t/javascript-arguments/14283)

* * *

## Hints

### Hint 1

Remember to iterate through the first argument to check each object.

### Hint 2

Only if all of them are truthy will we return true, so make sure all of them check.

### Hint 3

You could use loops or callback functions, there are multiple ways to solve this problem.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> **Using for-in loop & hasOwnProperty**
> 
> ```javascript
> function truthCheck(collection, pre) {
> // Create a counter to check how many are true.
> let counter = 0;
> // Check for each object
> for (let c in collection) {
> // If it is has property and value is truthy
> if (collection[c].hasOwnProperty(pre) && Boolean(collection[c][pre])) {
> counter++;
> }
> }
> // Outside the loop, check to see if we got true for all of them and return true or false
> return counter == collection.length;
> }
> 
> truthCheck([{ name: "Quincy", role: "Founder", isBot: false }, { name: "Naomi", role: "", isBot: false }, { name: "Camperbot", role: "Bot", isBot: true }], "isBot");
> 
> ```
> 
> #### Code Explanation
> 
> - First I create a counter to check how many cases are actually true.
> - Then check for each object if the value is truthy
> - Outside the loop, I check to see if the counter variable has the same value as the length of **collection** , if true then return **true** , otherwise, return **false**
> 
> #### Relevant Links
> 
> - [JS Loops](http://forum.freecodecamp.com/t/javascript-loops/14681)
> - [Object.prototype.hasOwnProperty()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)

> **Solution 2 (Click to Show/Hide)**
>
> **Using Array.every()**
> 
> ```javascript
> function truthCheck(collection, pre) {
> return collection.every(function (element) {
> return element.hasOwnProperty(pre) && Boolean(element[pre]);
> });
> }
> 
> truthCheck([{ name: "Quincy", role: "Founder", isBot: false }, { name: "Naomi", role: "", isBot: false }, { name: "Camperbot", role: "Bot", isBot: true }], "isBot");
> 
> ```
> 
> #### Code Explanation
> 
> - Uses the native “every” method to test whether all elements in the array pass the test.
> - This link will help [Array.prototype.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
> 
> #### Relevant Links
> 
> - [JS Array.prototype.every()](http://forum.freecodecamp.com/t/javascript-array-prototype-every/14287)
> - [MDN Array.prototype.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> function truthCheck(collection, pre) {
> // Is everyone being true?
> return collection.every(obj => obj[pre]);
> }
> 
> truthCheck([{ name: "Quincy", role: "Founder", isBot: false }, { name: "Naomi", role: "", isBot: false }, { name: "Camperbot", role: "Bot", isBot: true }], "isBot");
> 
> ```
> 
> #### Code Explanation
> 
> - For _every_ object in the `collection` array, check the truthiness of object’s property passed in `pre` parameter
> - `Array.prototype.every` method internally checks if the value returned from the callback is truthy.
> - Return `true` if it passes for _every_ object. Otherwise, return `false`.
> 
> #### Relevant Links
> 
> - [`Array.prototype.every`](http://devdocs.io/javascript/global_objects/array/every)

---

<div class="post-metadata">

**Author:** ![augorji](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@augorji](https://forum.freecodecamp.org/u/augorji)\
**Post date:** [August 3, 2016, 7:29pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/2 "2016-08-03T19:29:47Z")

</div>

It seems that you do not need to hasOwnProperty because if it does not have the property then it will return a falsey anyways. Thus my below solution worked. Thanks for the hard work friend. I like to read your solution after so I can see how to do it more efficiently/get exposed to more advanced code.

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

---

<div class="post-metadata">

**Author:** ![sa-mm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sa-mm/32/51904_2.png) [@sa-mm](https://forum.freecodecamp.org/u/sa-mm)\
**Post date:** [August 3, 2016, 8:16pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/3 "2016-08-03T20:16:45Z")

</div>

Another possible solution:

```auto
function truthCheck(collection, pre) {
  // Is everyone being true?
  return collection.every(function (element) {
    return element[pre];
  });
}

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

```

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 10, 2016, 6:44pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/4 "2016-08-10T18:44:28Z")

</div>



---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 20, 2016, 3:41am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/5 "2016-08-20T03:41:35Z")

</div>



---

<div class="post-metadata">

**Author:** ![Brada007](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/brada007/32/16557_2.png) [@Brada007](https://forum.freecodecamp.org/u/Brada007)\
**Post date:** [December 28, 2016, 3:49pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/7 "2016-12-28T15:49:09Z")

</div>

```
function truthCheck(collection, pre) {
var bool = false;  
  for (var each in collection) {
    if (collection[each][pre]) {
      bool=true;
    }
    else {
      return false;
    }
  }
 return bool;
}

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

---

<div class="post-metadata">

**Author:** ![raovikash](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@raovikash](https://forum.freecodecamp.org/u/raovikash)\
**Post date:** [January 22, 2017, 5:53pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/8 "2017-01-22T17:53:06Z")

</div>

> [@augorji](#):
>
> collection[i][pre]

Instead of using above code if you want to use this-  
collection[i].pre  
then it doesn’t pass all testcases.Can you please give any reason?

---

<div class="post-metadata">

**Author:** ![anupammajhi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anupammajhi/32/27949_2.png) [@anupammajhi](https://forum.freecodecamp.org/u/anupammajhi)\
**Post date:** [January 22, 2017, 8:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/9 "2017-01-22T20:26:51Z")

</div>

function truthCheck(collection, pre) {  
return collection.reduce(function(acc,cur){ return Boolean(cur[pre] && acc); },true);  
}

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

---

<div class="post-metadata">

**Author:** ![sa-mm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sa-mm/32/51904_2.png) [@sa-mm](https://forum.freecodecamp.org/u/sa-mm)\
**Post date:** [January 27, 2017, 9:04pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/10 "2017-01-27T21:04:51Z")

</div>

`pre` is a variable in this case, and you can’t use dot notation with variables. Writing `collection[i].pre` is like writing `collection[i]['pre']`. (Notice that in the latter “pre” is a string.).

---

<div class="post-metadata">

**Author:** ![sayedrakib](https://avatars.discourse-cdn.com/v4/letter/s/2acd7d/32.png) [@sayedrakib](https://forum.freecodecamp.org/u/sayedrakib)\
**Post date:** [February 9, 2017, 12:08pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/11 "2017-02-09T12:08:28Z")

</div>

```
function truthCheck(collection, pre) {
var res = collection.map(x => x[pre]);  
return res.every(p => p);
}
```

---

<div class="post-metadata">

**Author:** ![pennyJack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pennyjack/32/39751_2.png) [@pennyJack](https://forum.freecodecamp.org/u/pennyJack)\
**Post date:** [February 11, 2017, 10:15am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/13 "2017-02-11T10:15:39Z")

</div>

Can we please substitute the current “Basic Code Solution” with this one?

```javascript
function truthCheck(collection, pre) {
  for (var obj = 0; obj < collection.length; obj++) {
    //the if statement using || could also be written as two if statements!
    //actually the first expression of the condition is not required
    if (!(pre in collection[obj]) || !collection[obj][pre]) {
      return false;
    }
  }
  return true;
}

```

Augorji already came up with this at the very beginning of this post and in terms of readability, cleanliness, and efficiency, it’s just better in my opinion. It immediately returns false, if the condition isn’t met and does not run until the very end, unlike the current solution.

---

<div class="post-metadata">

**Author:** ![tonystaark](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@tonystaark](https://forum.freecodecamp.org/u/tonystaark)\
**Post date:** [May 17, 2017, 12:53pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/14 "2017-05-17T12:53:05Z")

</div>

```auto
function truthCheck(collection, pre) {
  for (var obj of arguments[0]){
    if (!obj[arguments[1]]){
      return false;
    }
  }
  return true;
}

```

Check out my code. Sweet and clean.

---

<div class="post-metadata">

**Author:** ![RyzorBent](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ryzorbent/32/54499_2.png) [@RyzorBent](https://forum.freecodecamp.org/u/RyzorBent)\
**Post date:** [July 4, 2017, 6:43am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/15 "2017-07-04T06:43:46Z")

</div>

```auto

function truthCheck(collection, pre) {
  // Is everyone being true?
  
var rs = collection.reduce(function(a, b){
return Boolean(a) && Boolean(b[pre]) ? true :false;
}, true);

  return rs;
}

```

---

<div class="post-metadata">

**Author:** ![cberigan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cberigan/32/161773_2.png) [@cberigan](https://forum.freecodecamp.org/u/cberigan)\
**Post date:** [July 7, 2017, 6:06pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/16 "2017-07-07T18:06:30Z")

</div>

I completed mine using reduce.

```auto
function truthCheck(collection, pre) {
  // Is everyone being true?
  return collection.reduce(function(r,v){
    return r && Boolean(v[pre]);
  }, true);
}

```

---

<div class="post-metadata">

**Author:** ![baseballpoetess](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/baseballpoetess/32/161715_2.png) [@baseballpoetess](https://forum.freecodecamp.org/u/baseballpoetess)\
**Post date:** [July 10, 2017, 1:17am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/17 "2017-07-10T01:17:15Z")

</div>

```
function truthCheck(collection, pre) {
  return !collection.some(function(i){
    return !(i[pre]);
  });
}
```

---

<div class="post-metadata">

**Author:** ![Venkateshwaran](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/venkateshwaran/32/39086_2.png) [@Venkateshwaran](https://forum.freecodecamp.org/u/Venkateshwaran)\
**Post date:** [July 17, 2017, 4:34pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/18 "2017-07-17T16:34:33Z")

</div>

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

```

This is my solution. I feel like I am missing something here, though it passed all the tests. feedback would be really appreciated.

---

<div class="post-metadata">

**Author:** ![codergy](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codergy/32/40781_2.png) [@codergy](https://forum.freecodecamp.org/u/codergy)\
**Post date:** [July 22, 2017, 7:14pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/19 "2017-07-22T19:14:10Z")

</div>

This was way easier than I thought 🙂

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

---

<div class="post-metadata">

**Author:** ![cndragn](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cndragn/32/159093_2.png) [@cndragn](https://forum.freecodecamp.org/u/cndragn)\
**Post date:** [July 23, 2017, 4:06pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/20 "2017-07-23T16:06:00Z")

</div>

Everyone else got these super short concise results… mine is a lot longer but it worked and Im proud of it (for now, in a couple months Im sure I will see this and cringe) anyway, here’s what I came up with.

```
function truthCheck(collection, pre) {
    // Is everyone being true?

    var colSize = Object.values(collection).length;
    var match = "";

    //find the matching key to find truthy value to compare
    for (i = 0; i < colSize; i++) {

        var keys = (Object.keys(collection[0])); // ['user', 'sex', 'age'])
        if (keys[i] === pre) {
            match = i;
            console.log(match);
        }
    }

    //iterate through collection and if key value is false, all are not truthy!
    for (i = 0; i < colSize; i++) {

        var colValue = Object.values(collection[i]); // ['Tinky-Winky', 'male', 0]
        if (!colValue[match]) {
        	console.log(false);
        	return false;
        }
        console.log(true);
            
    }

return true;
}
```

---

<div class="post-metadata">

**Author:** ![Master-Osaro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/master-osaro/32/161670_2.png) [@Master-Osaro](https://forum.freecodecamp.org/u/Master-Osaro)\
**Post date:** [July 24, 2017, 5:21pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/21 "2017-07-24T17:21:34Z")

</div>

```
function truthCheck(collection, pre) {
  // Is everyone being true?
  var bool = collection.every(function(element,index,array)
{
    if(element.hasOwnProperty(pre)&&element[pre]!=false&&element[pre]!=0,element[pre]!=""&& element[pre]!=null&&element[pre]!=undefined&&!Number.isNaN(element[pre])){
     return true;
    }
    return false;
  }
);
  return bool;
}
```

---

<div class="post-metadata">

**Author:** ![Master-Osaro](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/master-osaro/32/161670_2.png) [@Master-Osaro](https://forum.freecodecamp.org/u/Master-Osaro)\
**Post date:** [July 24, 2017, 5:24pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011/22 "2017-07-24T17:24:58Z")

</div>

> [@codergy](#):
>
> ```
> function truthCheck(collection, pre) {
>  
> for (var i = 0; i &lt; collection.length; i++ ) {
> if (!collection[i][pre]) {
> return false;
> }
> }
>   
> return true;
>   
> }
> 
> ```

I like this. So precise!

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-everything-be-true/16011.md?page=2)
