Be Brav3

FCC: Boo Who (JavaScript Intermediate Algorithm)

September 22, 2016

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

[js]

function booWho(bool) {

// What is the new fad diet for ghost developers? The Boolean.

return bool;

}

booWho(null);

[/js]

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

[js]

if (typeof bool === ‘boolean’) {

bool = true;

} else {

bool = false;

}

[/js]

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.

[js]

return typeof bool === ‘boolean’;

[/js]

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

[js]

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

[/js]