Skip to content

Commit 86d8ca9

Browse files
committed
Refactor getOrdinalNumber function to correctly return ordinal suffixes based on input number
1 parent b39a033 commit 86d8ca9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const str = String(num);
3+
4+
const lastTwo = num % 100;
5+
6+
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
7+
return str + "th";
8+
}
9+
const lastDigit = num % 10;
10+
if (lastDigit === 1) {
11+
return str + "st";
12+
} else if (lastDigit === 2) {
13+
return str + "nd";
14+
} else if (lastDigit === 3) {
15+
return str + "rd";
16+
} else {
17+
return str + "th";
18+
}
19+
20+
321
}
422

523
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)