Skip to content

Commit f76ff58

Browse files
committed
another way of doing getOrdinalNumber task. in a clear way
1 parent 1ec2362 commit f76ff58

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
function getOrdinalNumber(num) {
2-
const s = ["th", "st", "nd", "rd"];
3-
const lastDigits = num % 100;
4-
// Uses the index 1, 2, or 3 for st/nd/rd, defaults to 0 (th)
5-
return num + (s[(lastDigits - 20) % 10] || s[lastDigits] || s[0]);
2+
const lastTwoDigits = num % 100;
3+
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return num + "th";
6+
}
7+
switch (num % 10) {
8+
case 1:
9+
return num + "st";
10+
case 2:
11+
return num + "nd";
12+
case 3:
13+
return num + "rd";
14+
default:
15+
return num + "th";
16+
}
617
}
718

819
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)