Skip to content

Commit 253d984

Browse files
fix getOrdinalNumber function to handle invalid inputs and improve logic for ordinal suffixes
1 parent 7f9d151 commit 253d984

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
function getOrdinalNumber(num) {
2+
if (typeof num !== "number" ||!Number.isInteger(num) || num === 0 ) {
3+
return NaN;
4+
}
5+
26
const lastDigit=Number(num.toString().slice(-1));
37
const lastTwoDigits=Number(num.toString().slice(-2));
48

5-
if(typeof num !=="number"){
6-
return NaN;
7-
}
89

910
if(lastTwoDigits>=10 && lastTwoDigits<=13){
1011
return `${num}th`;
@@ -14,13 +15,13 @@ function getOrdinalNumber(num) {
1415
return `${num}st`;
1516
} else if (lastDigit === 2) {
1617
return `${num}nd`;
17-
} else if (num === 3) {
18-
return "3rd";
18+
} else if (lastDigit === 3) {
19+
return `${num}rd`;
1920
}
2021

2122
return `${num}th`;
2223

2324
}
24-
25+
console.log(getOrdinalNumber(-1))
2526
module.exports = getOrdinalNumber;
2627

0 commit comments

Comments
 (0)