2018-10-12 15:37:13 -04:00
---
title: Everything Be True
---
2019-07-24 00:59:27 -07:00
# Everything Be True
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
The program needs to check if the second argument is a < a href = 'http://forum.freecodecamp.com/t/javascript-truthy-value/15975' target = '_blank' rel = 'nofollow' > truthy< / a > element, and it must check this for each object in the first argument.
#### Relevant Links
* < a href = 'http://forum.freecodecamp.com/t/javascript-truthy-value/15975' target = '_blank' rel = 'nofollow' > JavaScript Truthy</ a >
* < a href = 'http://forum.freecodecamp.com/t/javascript-arguments/14283.md' target = '_blank' rel = 'nofollow' > JavaScript Arguments</ a >
2019-07-24 00:59:27 -07:00
---
## Hints
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
Remember to iterate through the first argument to check each object.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
Only if all of them are truthy will we return true, so make sure all of them check.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 3
2019-01-15 15:13:12 -08:00
You could use loops or callback functions, there are multiple ways to solve this problem.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
**Using for-in loop & hasOwnProperty**
2019-07-24 00:59:27 -07:00
```javascript
function truthCheck(collection, pre) {
// Create a counter to check how many are true.
var counter = 0;
// Check for each object
for (var c in collection) {
// If it is has property and value is truthy
if (collection[c].hasOwnProperty(pre) & & Boolean(collection[c][pre])) {
counter++;
2018-10-12 15:37:13 -04:00
}
2019-07-24 00:59:27 -07:00
}
// Outside the loop, check to see if we got true for all of them and return true or false
return counter == collection.length;
}
// test here
truthCheck(
[
{ user: "Tinky-Winky", sex: "male" },
{ user: "Dipsy", sex: "male" },
{ user: "Laa-Laa", sex: "female" },
{ user: "Po", sex: "female" }
],
"sex"
);
```
#### Code Explanation
2018-10-12 15:37:13 -04:00
* 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
* < a href = 'http://forum.freecodecamp.com/t/javascript-loops/14681' target = '_blank' rel = 'nofollow' > JS Loops</ a >
* < a href = 'https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty' target = '_blank' rel = 'nofollow' > Object.prototype.hasOwnProperty()</ a >
2019-07-24 00:59:27 -07:00
< / details >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 2 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
**Using Array.every()**
2019-07-24 00:59:27 -07:00
```javascript
function truthCheck(collection, pre) {
return collection.every(function(element) {
return element.hasOwnProperty(pre) & & Boolean(element[pre]);
});
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// test here
truthCheck(
[
{ user: "Tinky-Winky", sex: "male" },
{ user: "Dipsy", sex: "male" },
{ user: "Laa-Laa", sex: "female" },
{ user: "Po", sex: "female" }
],
"sex"
);
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
* Uses the native "every" method to test whether all elements in the array pass the test.
* This link will help < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every' target = '_blank' rel = 'nofollow' > Array.prototype.every()</ a >
#### Relevant Links
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-every/14287' target = '_blank' rel = 'nofollow' > JS Array.prototype.every()</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every' target = '_blank' rel = 'nofollow' > MDN Array.prototype.every()</ a >
2019-07-24 00:59:27 -07:00
< / details >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 3 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
```javascript
function truthCheck(collection, pre) {
// Is everyone being true?
return collection.every(obj => obj[pre]);
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
truthCheck(
[
{ user: "Tinky-Winky", sex: "male" },
{ user: "Dipsy", sex: "male" },
{ user: "Laa-Laa", sex: "female" },
{ user: "Po", sex: "female" }
],
"sex"
);
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
* For _every_ object in the `collection` array, check the truthiness of object's property passed in `pre` parameter
* `Array#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
* < a href = 'http://devdocs.io/javascript/global_objects/array/every' target = '_blank' rel = 'nofollow' > `Array#every` </ a >
2019-07-24 00:59:27 -07:00
< / details >