freeCodeCamp Algorithm Challenge Guide: Boo Who

freeCodeCamp Algorithm Challenge Guide: Boo Who
0.0 0

#1

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

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.

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

Hint: 2

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

try to solve the problem now

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

Basic Code Solution:

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

// test here
booWho(null);
``````

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`.

NOTES FOR CONTRIBUTIONS:

• 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.

See `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)

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

booWho(null);

``````

#10

intermediate?? Shhmmmehh :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);