We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 14b3b78 commit 74a0212Copy full SHA for 74a0212
Sprint-3/2-practice-tdd/get-ordinal-number.js
@@ -1,5 +1,23 @@
1
function getOrdinalNumber(num) {
2
- return "1st";
+ const last2Digits = String(num).slice(-2);
3
+ if (["11", "12", "13"].includes(last2Digits)) return `${num}th`;
4
+
5
+ const lastDigit = String(num).slice(-1);
6
+ let ordinalResult = "";
7
+ switch (lastDigit) {
8
+ case "1":
9
+ ordinalResult = `${num}st`;
10
+ break;
11
+ case "2":
12
+ ordinalResult = `${num}nd`;
13
14
+ case "3":
15
+ ordinalResult = `${num}rd`;
16
17
+ default:
18
+ ordinalResult = `${num}th`;
19
+ }
20
+ return ordinalResult;
21
}
22
23
module.exports = getOrdinalNumber;
0 commit comments