I like the boolean trick and I like the idea of only having 4 strings in the array
Code:
function num_abbrev_str(n)
{
return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10];
}
In this 60% of the time the
(n%10>3)
will be true so the
Math.floor(n%100/10)==1
will only be executed for 40% of the numbers.
Invert the boolean and do the multiply
NOTE: I have utilised operator precedence to reduce char count.
Here is a version with parentheses to show precedence
PHP Code:
function num_abbrev_str(n)
{
return n + ["th","st","nd","rd"][(!( ((n%10) >3) || (Math.floor(n%100/10)==1)) ) * (n%10)];
}