DEV Community

Viren B
Viren B

Posted on • Updated on • Originally published at virenb.cc

Solving "Boo who" / freeCodeCamp Algorithm Challenges

Post can also be found on my website https://virenb.cc/fcc-010-boo-who

'Boo who' Challenge

Let's solve freeCodeCamp's Basic Algorithm Scripting Challenge, "Boo who"

Our Starter Code (& Tests)

function booWho(bool) {
  return bool;
}

booWho(null);

// Tests
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.

Our Instructions

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

Boolean primitives are true and false.

Thoughts

  • The argument's data types vary. Some booleans, strings, arrays, functions, etc.
  • After reading the instructions and tests a few times, we must narrow in on true or false inputs/arguments only.
  • We have to return a boolean, true or false.

Further Thoughts

Reading the instructions again, the challenge is asking us to return true for boolean primatives.

(Looking at the tests, booWho(false) must return true.)

So, we must write a function, which returns true if the input is a true or false. If it is any other value, we must return false.

There is a built in operator in JavaScript, typeof which returns the data type.

MDN documentation: typeof

Some pseudo pseudocode:

booWho(input) {
    if input type is true or false
        return true
    else
        return false    
}

We are just checking the typeof of the argument.

Solution

[SPOILER: SOLUTION TO CODE BELOW]

function booWho(bool) {
  return typeof bool == 'boolean';
}

Links & Resources

'Boo who' Challenge on fCC

freeCodeCamp

Donate to FCC!

Solution on my GitHub

Thank you for reading!

Top comments (0)