javascript add nd st th to number

Solutions on MaxInterview for javascript add nd st th to number by the best coders in the world

showing results for - "javascript add nd st th to number"
Johanna
24 Oct 2019
1function ordinal(number) {
2    const english_ordinal_rules = new Intl.PluralRules("en", {
3        type: "ordinal"
4    });
5    const suffixes = {
6        one: "st",
7        two: "nd",
8        few: "rd",
9        other: "th"
10    }
11    const suffix = suffixes[english_ordinal_rules.select(number)];
12    return (number + suffix);
13}
14
15ordinal(3); /* output: 3rd */
16ordinal(111); /* output: 111th */
17ordinal(-1); /* output: -1st */