Abstract
In the head.asp there’s a function called getsuffix (although it should be getSuffix for consistency and the reference to it in the function after should be changed). This function is a bit bloated and restrictive.
Current Code
I’ve seriously compacted the formatting of the code for brevity:
function getsuffix(number) {if (number == 1) {return "st";}else if (number == 21) {return "st";}else if (number == 31) {return "st";}else if (number == 2) {return "nd";}else if (number == 22) {return "nd";}else if (number == 3) {return "rd";}else if (number == 23) {return "rd";}else {return "th";}}
Solution
Some elegant replacement code for it is:
function getSuffix(d) {d = String(d);return d.substr(-(Math.min(d.length, 2))) > 3 && d.substr(-(Math.min(d.length, 2))) > 21 ? "th" : ["th", "st", "nd", "rd", "th"][Math.min(Number(d)%10, 4)];}
It’s a little less readable, but saves looping through the various if conditions. If you ever add something which requires suffixes for numbers greater than 31 ("That’s his 43rd century this season!", "Jargon Busters are currently 86th in the promotion tables") then this function covers that.
Explanation
Changes the number to a string, checks that that the last 1 or 2 digits is between 3 and 21 exclusive and returns ‘th’. Else it gives the correct suffix from an array, with the array position given by the remainder when the number is divided by ten, for the first four values.