Bi-Weekly Life Update (19!)

I was working on a freeCodeCamp challenge last night, and some things happened that I thought were worth writing about.

For this challenge (Everything Be True) I was asked to write a function that would take two arguments, an array of objects, and a property. If the property was ‘truthy’ [ I still struggled with that a bit until now!] for each object, the function should return True, but if any object either didn’t contain the property, or it if was ‘falsey’ [null, undefined, or 0] the function should return False.

This is an example test from the exercise:
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");

The function I write should return False because even though each object has the age property, the first object [Tinky-Winky] has an age value of zero.

A couple of things tripped me up even before I started. Firstly, I wasn’t clear on the difference between properties and keys were. A property is made up of key value pairs: it is made up of both the key and the value. I don’t know why this wasn’t clear to me before, really! That misunderstanding paired with the wording of the challenge made this a bit more complicated than it should have been! Secondly, my knowledge of ES6 is pretty basic. It’s something that I’ve seen parts of, but I hadn’t really taken the time to do any courses or anything to familiarise myself with it, or better my understanding of it.

The function that I initially wrote looked like this:

function truthCheck(collection, pre) {
  var res;
    for( var use of collection) {
      if (!use.hasOwnProperty(pre) || !use[pre]) {
        res = false;
      } else {
        res = true;
  return res;

I started out using the ES6 for…of loop to iterate through each object in the array. Then I checked each object to see if the property was missing, or if it was falsey. The function made all the tests pass, and the logic made sense, but it was pretty messy- I could see that as soon as I wrote it!

I used the hasOwnProperty pretty much because the word property was thrown around so much in the challenge description. There weren’t any clues offered- sometimes there are links to articles about method you should check out, but there wasn’t anything to point you in the right direction here.

The first thing I changed was the if statement- I got rid of the first part of the condition because if the property didn’t exist, it would be undefined, so return false- exactly what the second part of the condition was asking! That tidied up the code a fair bit already! Then I started looking around to see if there were methods which would test something with a function and return either true or false. Jackpot. every() does exactly what I needed it to do! It only return true if every item from an array returns true. The MDN page was super informative, and after a couple of minutes reading and playing around with it, it made  a huge improvement to my function! The last thing I wanted to change was that I wanted to use one of those fancy ES6 arrow expressions. Again, a little playing around and some reading ensued, then a little more reading when my function stopped working… Then I got it back again!

All in all, I spent maybe fifteen minutes to get my function from it’s first iteration to this:

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

Much better, right? Looks like I’ll be reading more about ES6!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s