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
Hints
Hint 1
You will need to check for the type of the parameter to see if it is a boolean.
Hint 2
To check for the type of a parameter, you can use typeof
.
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.
Solutions
Solution 1 (Click to Show/Hide)
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
.
Relevant Links
58 Likes
Why not this way?
function booWho(bool) {
return (bool === true || bool === false);
}
72 Likes
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;
}
15 Likes
I did this beautiful piece of work:
if(bool === false){
return true;
}
return bool === true;
}
34 Likes
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.
13 Likes
Did exactly the same thing (smh)
TeeJay
June 24, 2017, 8:57pm
9
function booWho(bool) {
if (bool === true || bool === false){
return true;
}
else{
return false;
}
}
booWho(null);
34 Likes
intermediate?? Shhmmmehh :stuck_out_tongue
function booWho(bool) {
var res = (bool === true || bool === false) ? (true) : (false);
return res;
}
8 Likes
I wish we had haha reactions…
7 Likes
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);
7 Likes
function booWho(bool) {
// What is the new fad diet for ghost developers? The Boolean.
return !!bool === bool;
}
booWho(null);
6 Likes
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);
6 Likes
baersc
July 19, 2017, 10:23pm
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;
}
5 Likes
function booWho(bool) {
return typeof(bool) === “boolean”;
}
booWho(false);
2 Likes
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);
1 Like
amyngb
July 23, 2017, 5:16pm
18
I did the exact same thing, too!
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);
}
3 Likes
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);
1 Like