Skip to content

Commit 27cd89c

Browse files
Alex JamshidiAlex Jamshidi
authored andcommitted
pr changes made
1 parent 4b38122 commit 27cd89c

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
function getOrdinalNumber(num) {
2-
if (!Number.isInteger(num) || num < 1) {throw new Error("Invalid number");}
3-
4-
let number = num.toString().slice(-2);
5-
if (number == 11 || number == 12 || number == 13) {return `${num}th`;}
6-
if (number.slice(-1) == 1) {return `${num}st`;}
7-
if (number.slice(-1) == 2) {return `${num}nd`;}
8-
if (number.slice(-1) == 3) {return `${num}rd`;}
9-
else return `${num}th`;
2+
if (!Number.isInteger(num) || num < 1) {
3+
throw new Error("Invalid number");
4+
}
105

6+
const lastTwoDigits = num.toString().slice(-2);
7+
const lastDigit = lastTwoDigits.slice(-1);
8+
if (lastTwoDigits == 11 || lastTwoDigits == 12 || lastTwoDigits == 13) {
9+
return `${num}th`;
10+
}
11+
if (lastDigit == 1) {
12+
return `${num}st`;
13+
}
14+
if (lastDigit == 2) {
15+
return `${num}nd`;
16+
}
17+
if (lastDigit == 3) {
18+
return `${num}rd`;
19+
} else return `${num}th`;
1120
}
1221

1322
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)