freeCodeCamp Algorithm Challenge Guide: Boo Who

freeCodeCamp Algorithm Challenge Guide: Boo Who
0

#1

:triangular_flag_on_post: Remember to use Read-Search-Ask if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:

Problem Explanation:

This program is very simple, the trick is to understand what a boolean primitive is. The programs requires a true or false answer.

Relevant Links

:speech_balloon: Hint: 1

  • You will need to check for the type of the parameter to see if it is a boolean.

try to solve the problem now

:speech_balloon: Hint: 2

  • To check for the type of a parameter, you can use typeof.

try to solve the problem now

:speech_balloon: Hint: 3

  • Since you must return true or false you can use if statements or just have it return the boolean used for the if statement.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:beginner: Basic Code Solution:

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

// test here
booWho(null);

:rocket: Run Code

Code Explanation:

Uses the operator typeof to check if the variable is a boolean. If it is, it will return true. Otherwise, if it is any other type it will return false.

Relevant Links

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.


#2

#3

#4

Why not this way?

function booWho(bool) {
  return (bool === true || bool === false);
}

#5

Can be performed using the builtin Boolean Function - only primitives remain equal to themselves with all other values converted to a booleans.

function booWho(bool) {

  return Boolean(bool)===bool;
}

#6

I did this beautiful piece of work:

  if(bool === false){
    return true;
  }
  return bool === true;
}

#7

I wrote more code than necessary at first. Here was my initial code:

function booWho(bool) {
if(typeof(bool) === “boolean”){
return true;
} else {
return false;
}
}

I realized the if statement is unnecessary, because the strict equality operator returns a true or false.


#8

Did exactly the same thing (smh):stuck_out_tongue_winking_eye:


#9

function booWho(bool) {
  if (bool === true || bool === false){
    return true;
  }
  else{
    return false; 
  }
}

booWho(null);


#10

intermediate?? Shhmmmehh :stuck_out_tongue :stuck_out_tongue:

function booWho(bool) {
  var res = (bool === true || bool === false) ? (true) : (false);
  return res;
}

#11

I wish we had haha reactions…


#12

I made it like this:

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

booWho(null);

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

booWho(null);

#14

Thought I was pretty clever to come up with my simple solution until I realised it was supposed to be a simple answer:(

function booWho(bool) {
  // What is the new fad diet for gho
  if (typeof bool !== "boolean"){ return false;}
  return true;
}

booWho(null);


#15

I didn’t remember that typeof returned a string, so I couldn’t figure out the suggested solution.
Instead I did something a little overly complicated,

function booWho(bool) {
  let test = true;
  return typeof bool === typeof test;
}

#16

function booWho(bool) {
return typeof(bool) === “boolean”;
}
booWho(false);


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

#18

I did the exact same thing, too!


#19

A very basic solution is to check if the input is one of true or false

function booWho(bool) {
return ([true,false].indexOf(bool) > -1);
}


#20

This one was pretty easy. Although some of these other answers are pretty genius.

function booWho(bool) {
var thingy = Boolean(bool);
if (bool !== thingy){return false;}
return true;
}

booWho(false);