FCC: Boo Who (JavaScript Intermediate Algorithm)

In FCC challenge Boo Who we’re trying to determine values being passed into the booWho function is a boolean.

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

booWho(null);

To determine whether the value in a variable is boolean or another type we can use the typeof operator. We can use the typeof operator and compare the variable to ‘boolean’.

if (typeof bool === 'boolean') {
  bool = true;
} else {
  bool = false;
}

Looking at Rafase282’s solution, he had a much simpler solution. He used the following code to return true of false for a boolean type.

return typeof bool === 'boolean';

typeof already returns true or false in my if condition so there was no need to assign true of false to bool and then returning it.

My full code is below

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

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');

Leave a Comment