Skip to content

Commit f1b150e

Browse files
committed
Fix ordinal suffix logic and tests
1 parent 2f7891c commit f1b150e

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
export function getOrdinalNumber(num) {
2-
return "1st";
2+
const absNum = Math.abs(num);
3+
const lastTwoDigits = absNum % 100;
4+
const lastDigit = absNum % 10;
5+
6+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
7+
return `${num}th`;
8+
}
9+
10+
if (lastDigit === 1) {
11+
return `${num}st`;
12+
}
13+
14+
if (lastDigit === 2) {
15+
return `${num}nd`;
16+
}
17+
18+
if (lastDigit === 3) {
19+
return `${num}rd`;
20+
}
21+
22+
return `${num}th`;
323
}

0 commit comments

Comments
 (0)