I have a function that returns a value that can be true if the condition is met and false if not, but the function can also return a string message in case of an error.
I need to differentiate between the true/false boolean values under normal conditions without mistaking the string value for either one. My strategy is to use a parseBoolean() function that will return a true Boolean-typed true/false value when passed a boolean input, but a “falsy” value that isn’t a Boolean-typed false when passed a string.
Example
function validate(kkk)
{
//... some check that validates
return true;
//... some check that doesn't validate
return false;
//... failure - return explanation
return 'Error Message jjjjjjjj';
}
function usingit(data)
{
if(parseBoolean(validate(data)) != false)
{
/// the value is Boolean true
}
else
{
if(parseBoolean(validate(data)) === false)
{
/// the value is Boolean false
}
else
{
/// the value is false but not of a Boolean type
/// so we will display it as the error message text.
}
}
}
…but I haven’t figured out how to create a parseBoolean() function that behaves this way. Any ideas?
According to this:
You want
But this obviously won’t pass the test of doom.
This code passes all your tests: