Skip to content

Commit de7beb8

Browse files
added code to the getordinalfunction to cover all possibilities
1 parent 9af1dec commit de7beb8

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit=Number(num.toString().slice(-1));
3+
const lastTwoDigits=Number(num.toString().slice(-2));
4+
5+
if(typeof num !=="number"){
6+
return NaN;
7+
}
8+
9+
if(lastTwoDigits>=10 && lastTwoDigits<=13){
10+
return `${num}th`;
11+
}
12+
13+
if (lastDigit === 1) {
14+
return `${num}st`;
15+
} else if (lastDigit === 2) {
16+
return `${num}nd`;
17+
} else if (num === 3) {
18+
return "3rd";
19+
}
20+
21+
return `${num}th`;
22+
323
}
424

525
module.exports = getOrdinalNumber;
26+

0 commit comments

Comments
 (0)