Skip to content

Commit a6bbe86

Browse files
committed
Fix getOrdinalNumber to handle integers correctly and reject invalid inputs
1 parent 14306cc commit a6bbe86

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed
Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
function getOrdinalNumber(num) {
2-
if (num === 1 || num === 21) {
3-
return `${num}st`;
4-
} else if (num === 11) {
5-
return `${num}th`;
6-
} else if (num === 2) {
7-
return `${num}nd`;
2+
let ordinalNum;
3+
if (Number.isInteger(num) === true) {
4+
if (
5+
Math.abs(num % 100) === 11 ||
6+
Math.abs(num % 100) === 12 ||
7+
Math.abs(num % 100) === 13 ||
8+
Math.abs(num % 10) === 4 ||
9+
Math.abs(num % 10) === 5 ||
10+
Math.abs(num % 10) === 6 ||
11+
Math.abs(num % 10) === 7 ||
12+
Math.abs(num % 10) === 8 ||
13+
Math.abs(num % 10) === 9 ||
14+
Math.abs(num % 10) === 0
15+
) {
16+
ordinalNum = `${num}th`;
17+
} else if (Math.abs(num % 10) === 1) {
18+
ordinalNum = `${num}st`;
19+
} else if (Math.abs(num % 10) === 2) {
20+
ordinalNum = `${num}nd`;
21+
} else if (Math.abs(num % 10) === 3) {
22+
ordinalNum = `${num}rd`;
23+
} else {
24+
ordinalNum = `${num}th`;
25+
}
26+
} else {
27+
ordinalNum = "Invalid input: Input is an integer";
828
}
29+
return ordinalNum;
930
}
10-
11-
1231
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)