loading...

Solving "Everything Be True" / freeCodeCamp Algorithm Challenges

virenb profile image Viren B Originally published at virenb.cc ・5 min read

Post can also be found on virenb.cc

'Everything Be True' Challenge

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

Starter Code

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,

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.

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().

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, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, -0, 0n, "", null, undefined, and NaN).

-Mozilla Developer Network

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()

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.

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]:

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

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

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].

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()

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.

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

That is all!

Our Solution

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

freeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

Posted on by:

virenb profile

Viren B

@virenb

Self-taught developer, always looking to learn more.

Discussion

markdown guide
 
checkedArr.includes(false) ? false : true

is equivalent to:

! checkedArr.includes(false)

It's like something verbose.

And , here another way:

const truthCheck = (collection, pre) =>
  collection.every(
    e => e[pre]
  )