Skip to content

Commit 7d1faf5

Browse files
committed
last one for practice TDD
1 parent 205610d commit 7d1faf5

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (num === 0) {
3+
return "0";
4+
}
5+
const lastTwoDigits = num % 100;
6+
const lastDigit = num % 10;
7+
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+
}
20+
return `${num}th`;
321
}
422

5-
module.exports = getOrdinalNumber;
23+
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,28 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(132)).toEqual("132nd");
26+
});
27+
28+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(23)).toEqual("23rd");
31+
expect(getOrdinalNumber(133)).toEqual("133rd");
32+
});
33+
34+
test("should not append any suffix for the number 0 and return 0", () => {
35+
expect(getOrdinalNumber(0)).toEqual("0");
36+
});
37+
38+
test("should append 'th' for numbers ending with 4-9 or 0, including those ending with 11-13", () => {
39+
expect(getOrdinalNumber(4)).toEqual("4th");
40+
expect(getOrdinalNumber(10)).toEqual("10th");
41+
expect(getOrdinalNumber(11)).toEqual("11th");
42+
expect(getOrdinalNumber(12)).toEqual("12th");
43+
expect(getOrdinalNumber(13)).toEqual("13th");
44+
expect(getOrdinalNumber(200)).toEqual("200th");
45+
});

0 commit comments

Comments
 (0)