Skip to content

Commit 94b050b

Browse files
committed
Finished get ordinal numberFinished get-ordinal number and the test
1 parent 844e904 commit 94b050b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const str = num.toString();
3+
const lastTwo = str.slice(-2);
4+
const lastDigit = str.slice(-1);
5+
6+
let suffix;
7+
if (lastTwo === '11' || lastTwo === '12' || lastTwo === '13') {
8+
suffix = 'th';
9+
} else if (lastDigit === '1') {
10+
suffix = 'st';
11+
} else if (lastDigit === '2') {
12+
suffix = 'nd';
13+
} else if (lastDigit === '3') {
14+
suffix = 'rd';
15+
} else {
16+
suffix = 'th';
17+
}
18+
19+
return str + suffix;
320
}
421

522
module.exports = getOrdinalNumber;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,34 @@ 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+
// Case 2: Numbers ending with 2 (but not 12)
23+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(132)).toEqual("132nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(133)).toEqual("133rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12, 13, or any other digit
41+
// When the number ends with 11, 12, 13, or any digit not 1, 2, or 3,
42+
// Then the function should return a string by appending "th" to the number.
43+
test("should append 'th' for numbers ending with 11, 12, 13, or other digits", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
expect(getOrdinalNumber(4)).toEqual("4th");
48+
expect(getOrdinalNumber(5)).toEqual("5th");
49+
expect(getOrdinalNumber(10)).toEqual("10th");
50+
expect(getOrdinalNumber(111)).toEqual("111th");
51+
});

0 commit comments

Comments
 (0)