**Ordinal numbers**are the word representations of the rank of a number with respect to its order.

Here we are going to convert those numeral (e.g. 1, 2, 3, etc.) values to it's ordinal equivalent.

### Sample and Analysis

By mathematical rule:- if a numeral ends with
**1**, the number is followed by "**st**" - if a numeral ends with
**2**, the number is followed by "**nd**" - if a numeral ends with
**3**, the number is followed by "**rd**" - else, the number is followed by "
**th**"

Thus it should look like this:

1 = 1st

2 = 2nd

11 = 11th

20 = 20th

31 = 31st

101 = 101st

### The JavaScript Code

var ordinalize = function(number){ number += ''; if(isNaN(number)) return number; if(number.length === 1){ if (number === '1') return number + "st"; if (number === '2') return number + "nd"; if (number === '3') return number + "rd"; if (number != '1'&& number != '2' && number != '3') return number + "th" } else { secLast = number[number.length - 2]; lastLast = number[number.length - 1]; if (secLast === '1') return number + 'th'; else { if (lastLast === '1') return number + "st"; if (lastLast === '2') return number + "nd"; if (lastLast === '3') return number + "rd"; if (lastLast != '1' && lastLast != '2' && lastLast != '3') return number + "th" } } };

### Usage

To use simply call`ordinalize(NUMBER)`

and it will return the ordinalize version. If in case a string was passed, the same value would be return.
## No comments :

## Post a Comment