Skip to content

Commit a4b2b74

Browse files
refactor getOrdinalNumber function to improve input validation structure
1 parent eee22d9 commit a4b2b74

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
function getOrdinalNumber(num) {
2-
if (typeof num !== "number" ||!Number.isInteger(num) || num === 0 ) {
2+
if (typeof num !== "number") {
33
return NaN;
44
}
5-
6-
const lastDigit=Number(num.toString().slice(-1));
7-
const lastTwoDigits=Number(num.toString().slice(-2));
5+
if(!Number.isInteger(num)){
6+
return "not an integer number";
7+
}
8+
if(num === 0 ){
9+
return "invalid number";
10+
}
811

12+
const lastDigit = Number(num.toString().slice(-1));
13+
const lastTwoDigits = Number(num.toString().slice(-2));
914

10-
if(lastTwoDigits>=10 && lastTwoDigits<=13){
15+
if (lastTwoDigits >= 10 && lastTwoDigits <= 13) {
1116
return `${num}th`;
1217
}
1318

@@ -18,10 +23,8 @@ function getOrdinalNumber(num) {
1823
} else if (lastDigit === 3) {
1924
return `${num}rd`;
2025
}
21-
22-
return `${num}th`;
2326

27+
return `${num}th`;
2428
}
25-
console.log(getOrdinalNumber(-1))
26-
module.exports = getOrdinalNumber;
2729

30+
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)