Let me count the ways:
>>> 1 ? "yes" : "no"
"yes"
>>> 0 ? "yes" : "no"
"no"
>>> "1" ? "yes" : "no"
"yes"
>>> "0" ? "yes" : "no"
"yes"
>>> Number("1") ? "yes" : "no"
"yes"
>>> Number("0") ? "yes" : "no"
"no"
>>> Number("true") ? "yes" : "no"
"no"
function DoWhatIMean( pString )
{
if ( typeof pString === 'string' )
{
return pString.toLowerCase() == "true" ?
true : Number(pString) ? true : false
}
else
{
return Number(pString) ? true : false
}
}
>>> DoWhatIMean( "true" ) ? "yes" : "no"
"yes"
>>> DoWhatIMean( "false" ) ? "yes" : "no"
"no"
Welcome to 2007.
Note, I even originally had two rookie mistakes in “DoWhatIMean” - did not “*.toLowerCase()” it, and also did not check to see if it was a string first. Ugh.
13:25 CST | category / entries
permanent link | comments?