Finish the function “numberToOrdinal”, which should take a number and return it as a string with the correct ordinal indicator suffix (in English). For example, “1” turns into “1st”, 2 to 2nd, 3 to 3rd and 4 to 4th. Below is my code
function numberToOrdinal(n) {
var j = n % 10;
k = n % 100;
if (j == 0 && n == 0) {
return 0;
}
if (j == n && k != 11) {
return n + "st";
}
if (j == 2 && k != 12) {
return n + "nd";
}
if (j == 3 && k != 13) {
return n + "rd";
}
return n + "th";
}
Unfortunately the code did not pass the following requirement;
1.) should handle single digits
expected ‘2st’ to equal ‘2nd’
2.) should handle larger double digits
expected ‘21th’ to equal ‘21st’
3.) should handle the largest cases
expected ‘101th’ to equal ‘101st’