Solving "Boo who" / freeCodeCamp Algorithm Challenges
Viren B
Posted on May 26, 2020
Post can also be found on my website https://virenb.cc/fcc-010-boo-who
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.
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
Thank you for reading!
Posted on May 26, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.