Skip to content

Commit 23a26c6

Browse files
committed
wrote the function getOrdinalNumber code
1 parent 7f2b357 commit 23a26c6

File tree

1 file changed

+15
-1
lines changed

1 file changed

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

519
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)