Skip to content

Commit 2256f87

Browse files
committed
In get-ordinal-number: if statement replaced by switch statement
1 parent 8222fda commit 2256f87

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
function getOrdinalNumber(num) {
2-
if (num === 0) {
2+
if (num === 0) {
33
return "0";
44
}
5+
56
const lastTwoDigits = num % 100;
67
const lastDigit = num % 10;
78

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`;
9+
switch (true) {
10+
case lastTwoDigits === 11:
11+
case lastTwoDigits === 12:
12+
case lastTwoDigits === 13:
13+
return `${num}th`;
14+
case lastDigit === 1:
15+
return `${num}st`;
16+
case lastDigit === 2:
17+
return `${num}nd`;
18+
case lastDigit === 3:
19+
return `${num}rd`;
20+
default:
21+
return `${num}th`;
1922
}
20-
return `${num}th`;
2123
}
2224

2325
module.exports = getOrdinalNumber;

0 commit comments

Comments
 (0)