Solving "Everything Be True" / freeCodeCamp Algorithm Challenges
Tags: freecodecamp, algorithms, challenge, javascript
Published: 2020-08-04 17:50:18

---
title: Solving "Everything Be True" / freeCodeCamp Algorithm Challenges
published: true
description: My guide, notes, and solution to freeCodeCamp's intermediate algorithm challenge, "Everything Be True" 
tags: #freeCodeCamp, #algorithms, #challenge, #javascript
canonical_url: https://virenb.cc/fcc-034-everything-be-true
series: Solving freeCodeCamp's Algorithm Challenges
---

**Post can also be found on [virenb.cc](https://virenb.cc "Post on virenb.cc")**

!['Everything Be True' Challenge](//images.ctfassets.net/yzwpq1epaq68/6cqeQsfUKC0ICwvIx67y6R/ae9a8f7861530787e98b0202166cbdb2/ss-034.png)

Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Everything Be True'.



### Starter Code

```javascript
function truthCheck(collection, pre) {
  return pre;
}

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



### Instructions

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.



### Test Cases (& Rules)

- `truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"},  {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}],  "sex")` should return true.
- `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([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias":  "Repete"}, {"name": "FastForward", "onBoat": true}], "onBoat")` should return true
- `truthCheck([{"single": "yes"}], "single")` should return true
- `truthCheck([{"single": ""}, {"single": "double"}], "single")` should return false
- `truthCheck([{"single": "double"}, {"single": undefined}], "single")` should return false
- `truthCheck([{"single": "double"}, {"single": NaN}], "single")` should return false



# Our Approach

After reading the instructions, starter code, and test cases more than once, this is what we're working with:

- We have two arguments, `collection`, an array of objects and, `pre`, a string.
- We need to return a boolean value, `true` or `false`.
- We need to check each object's properties and check if the string, `pre`, exists in the object, and if the value is a truthy value.



So, looking at one of the test cases, 



```javascript
truthCheck([{"single": "double"}, {"single": NaN}], "single")
// false
```



should be false because the property "single" is in the objects *but* the second item is `NaN`, a falsy value.



Since `collection` is already in an array, we can use a `for` loop or `map()` to iterate over the items. The challenging part is we have objects within the array.



All of the built-in methods of an Object are listed **[here on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods "MDN docs: Object")**.



I've found `Object.keys()` to be very helpful but we want to check the value of the key as well so it may not be the best way to approach this challenge.



We can start by writing out our `map()`.



```javascript
let checkedArrr = collection.map(obj => {
  // more code
})
```



So we want to check that the property, which is the word from `pre`, exists in `collection`. Then we want to check that all the values are truthy.



> In [JavaScript](https://developer.mozilla.org/en-US/docs/Glossary/JavaScript), a **truthy** value is a value that is considered `true` when encountered in a [Boolean](https://developer.mozilla.org/en-US/docs/Glossary/Boolean) context. All values are truthy unless they are defined as [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) (i.e., except for `false`, `0`, `-0`, `0n`, `""`, `null`, `undefined`, and `NaN`).

-[Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Glossary/Truthy "MDN Truthy JS")



We will find a way to check the value but we'll first check if the property exists. Within the documentation, there is a method called `hasOwnProperty()`, which does exactly what it sounds like. It will return a true or false value.



[MDN: Object.hasOwnProperty()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty "MDN Object hasOwnProperty() docs")



```javascript
function truthCheck(collection, pre) {
	let checkedArr = collection.map(obj => {
    return obj.hasOwnProperty(pre);
  })
	return checkedArr
}

truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastForward", "onBoat": true}], "onBoat")
// [ true, true, true ]
```



So we are half way there, we are able to check that the property 'onBoat' exists in every object in `collection`. We can see that every 'onBoat' property has a string value, so they are all truthy values. This should return true.



We have to find a way to access the value of the correct key in the object. We can do this with bracket notation.



```javascript
const team = [{name: 'Leo Messi', number: 10}, {name: 'Harry Kane', number: 10}, {name: 'Luuk De Jong', number: 19}];

team.map(object => {
  return object['name']
})
// [ "Leo Messi", "Harry Kane", "Luuk De Jong" ]
```



So in our challenge, we've written the below code. We can add in `obj[pre] `:



```javascript
function truthCheck(collection, pre) {
	let checkedArr = collection.map(obj => {
    return obj.hasOwnProperty(pre) && obj[pre]
  })
}
```



`obj[pre] ` would just give us the value, we want to check if it is truthy or falsy. Step in **Boolean**. It is another built-in object which you can wrap around a value to check if it is a boolean.

[MDN: Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean "MDN Boolean")



So to test out Boolean on our smaller array of objects example (we added a fourth member to our team):



```javascript
const team = [{name: 'Leo Messi', number: 10}, {name: 'Harry Kane', number: 10}, {name: 'Luuk De Jong', number: 19}, {name: '', number: 45}];

team.map(object => {
  return Boolean(object['name'])
})
// [ true, true, true, false ]
```



The fourth item did not have a name, just an empty string, so it returned false. Back to our challenge, we can wrap Boolean around `obj[pre]`.



```javascript
function truthCheck(collection, pre) {
	let checkedArr = collection.map(obj => {
    return obj.hasOwnProperty(pre) && Boolean(obj[pre]);
  })
}
```



So we're now able to return an array of true or false values. This is progress but we have to return a single true or false. If one object's value is falsy, we have to return false. So since the above code was giving us an array of true and falses, we can check if even one false exists, we can return false.



[MDN: Array.includes()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes "MDN Docs: includes()")



This method is a little new-er to me but it does the job we want here. We can run `checkedArr.includes()` to see if the array includes `false`. I think a ternary operator is a nice way to write this out.



```javascript
return checkedArr.includes(false) ? false : true;
```



That is all!



# Our Solution

```javascript
function truthCheck(collection, pre) {
  let checkedArr = collection.map(obj => {
    return obj.hasOwnProperty(pre) && Boolean(obj[pre]);
  })

  return checkedArr.includes(false) ? false : true;
}
```



# Links & Resources

['Everything Be True' Challenge on fCC](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true "Intermediate Algorithm Scripting: Everything Be True on fCC Website")

[freeCodeCamp](https://www.freecodecamp.org/, "freeCodeCamp")

[Donate to FCC!](https://www.freecodecamp.org/donate "fCC Donation Page")

[Solution on my GitHub](https://github.com/virenb/fcc-algorithms/blob/master/034---everything-be-true.js "GitHub Solution")



Thank you for reading!