Boo Who

In this freeCodeCamp algorithm we need to determine whether a number of different elements are considered to be boolean primitives. In short, boolean primitives return either true or false.

Note: It's important to point out that a boolean primitive is not a boolean object
To read up a little more on the difference, here is a helpful article

Requirements 📋

  • Check if a value is classified as a boolean primitive. Return true or false.

  • Boolean primitives are true and false.

  • booWho(true) should return true

  • booWho(false) should return true

  • booWho([1, 2, 3]) should return false

  • booWho([].slice) should return false

  • booWho({ "a": 1 }) should return false

  • booWho(1) should return false

  • booWho(NaN) should return false

  • booWho("a") should return false

  • booWho("true") should return false

  • booWho("false") should return false

Real-World Use Cases 🌎

Knowing whether a piece of data is either true or false, in some particular context, is one of the fundamentals of many software systems. For instance, you could use an algorithm like this to tell you things like isPaymentLate or doesAccountHaveSufficientFunds. Returning a primitve boolean from either of these gives you a clear and concise answer to an important question.

Psuedo-code

Let's walk through the steps we will need to take, without code, in order to pass the requirements listed above. After skimming through the requirements, you may have noticed that if bool is not literally true or false then we must return false.

// create a function that will accept a value as a parameter
    // check whether or not that parameter is literally true or false
        // if so, return true
       
    // otherwise, return false

Solving the Algorithm

Setting up the function

Let's use the boilerplate function that's provided to us by freeCodeCamp:

function booWho(bool) {
  // What is the new fad diet for ghost developers? The Boolean.
  return bool;
}

booWho(null);

Creating our logic

As mentioned above in the psuedo-code section, we need to determine whether or not the value passed into our function, in this case bool is literally true or false. If so, we will return true otherwise we will return false:

function booWho(bool) {
  // What is the new fad diet for ghost developers? The Boolean.
  
  if(bool === true || bool === false) {
    return true;
  } else {
    return false;
  }
}

booWho(null);
//booWho(true)
//booWho(false)
//booWho([1, 2, 3])
//booWho([].slice)
//booWho({ "a": 1 })
//booWho(1)
//booWho(NaN)
//booWho("a")
//booWho("true")
//booWho("false")

Test this code in your browser console

Recap 🔄

While solving this freeCodeCamp algorithm it was important that we made use of the strict equality comparison operator e.g. ===. This way we were able to determine whether bool was literally a true or false value, not just a truthy or falsy value.

Shoot me an email at tim@timwheeler.com with any questions and if you enjoyed this, stay tuned and subscribe below! 👇