add st nd rd th javascript

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

showing results for - "add st nd rd th javascript"
Michela
01 Jan 2020
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 */
Vanessa
03 Apr 2020
1The rules are as follows:
2
31. st is used with numbers ending in 1 (e.g. 1st, pronounced first)
42. nd is used with numbers ending in 2 (e.g. 92nd, pronounced ninety-second)
53. rd is used with numbers ending in 3 (e.g. 33rd, pronounced thirty-third)
6
7Note* : As an exception to the above rules, all the "teen" numbers ending with 11, 12
8or 13 use -th (e.g. 11th, pronounced eleventh, 112th, pronounced one hundred
9[and] twelfth)
10th is used for all other numbers (e.g. 9th, pronounced ninth).
11
12The following JavaScript code (rewritten in Jun '14) accomplishes this:
13
14function ordinal_suffix_of(i) {
15    var j = i % 10,
16        k = i % 100;
17    if (j == 1 && k != 11) {
18        return i + "st";
19    }
20    if (j == 2 && k != 12) {
21        return i + "nd";
22    }
23    if (j == 3 && k != 13) {
24        return i + "rd";
25    }
26    return i + "th";
27}