Ordinal Indicator/Suffix for numbers in Javascript

Input | Output

1 => 1st
2 => 2nd
3 => 3rd
4 => 4th
0 => Not Placed
21 => 21st
etc…


function ordinalSuffixGen(inputVal) {
var mod10 = inputVal % 10,
mod100 = inputVal % 100;
if (inputVal <= 0) {
return "Not placed";
}
if (mod10 == 1 && mod100 != 11) {
return inputVal + "st";
}
if (mod10 == 2 && mod100 != 12) {
return inputVal + "nd";
}
if (mod10 == 3 && mod100 != 13) {
return inputVal + "rd";
}
return inputVal + "th";
}

Advertisements