Skip to content

Commit f07827f

Browse files
committed
write function getOrdinalNumber()
1 parent 13e7832 commit f07827f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13)
4+
return `${num}th`;
5+
const lastDigit = num % 10;
6+
if (lastDigit === 1) return `${num}st`;
7+
if (lastDigit === 2) return `${num}nd`;
8+
if (lastDigit === 3) return `${num}rd`;
9+
return `${num}th`;
310
}
411

512
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)