Skip to content

Commit 150a497

Browse files
committed
Implement getOrdinalNumber function to return correct ordinal suffixes
1 parent f774e8d commit 150a497

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return `${num}th`;
7+
}
8+
9+
10+
if (lastDigit === 1) return `${num}st`;
11+
if (lastDigit === 2) return `${num}nd`;
12+
if (lastDigit === 3) return `${num}rd`;
13+
14+
return `${num}th`;
315
}
416

517
module.exports = getOrdinalNumber;
Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
1-
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber
1+
// Case 2: Identify the ordinal number for 2
2+
// When the number is 2,
3+
// Then the function should return "2nd"
4+
test("should return '2nd' for 2", () => {
5+
expect(getOrdinalNumber(2)).toEqual("2nd");
6+
});
7+
8+
// Case 3: Identify the ordinal number for 3
9+
// When the number is 3,
10+
// Then the function should return "3rd"
11+
test("should return '3rd' for 3", () => {
12+
expect(getOrdinalNumber(3)).toEqual("3rd");
13+
});
14+
15+
// Case 4: Identify ordinal numbers ending in 1 but not 11
16+
// When the number ends with 1 but is not 11,
17+
// Then the function should return the correct 'st' suffix
18+
test("should return '21st' for 21", () => {
19+
expect(getOrdinalNumber(21)).toEqual("21st");
20+
});
21+
22+
// Case 5: Identify ordinal numbers ending in 2 but not 12
23+
// When the number ends with 2 but is not 12,
24+
// Then the function should return the correct 'nd' suffix
25+
test("should return '42nd' for 42", () => {
26+
expect(getOrdinalNumber(42)).toEqual("42nd");
27+
});
328

4-
// continue testing and implementing getOrdinalNumber for additional cases
5-
// Write your tests using Jest - remember to run your tests often for continual feedback
29+
// Case 6: Identify ordinal numbers ending in 3 but not 13
30+
// When the number ends with 3 but is not 13,
31+
// Then the function should return the correct 'rd' suffix
32+
test("should return '53rd' for 53", () => {
33+
expect(getOrdinalNumber(53)).toEqual("53rd");
34+
});
635

7-
// Case 1: Identify the ordinal number for 1
8-
// When the number is 1,
9-
// Then the function should return "1st"
36+
// Case 7: Identify teen exceptions 11, 12, 13
37+
// When the number is 11, 12, or 13,
38+
// Then the function should return the correct 'th' suffix
39+
test("should return '11th' for 11", () => {
40+
expect(getOrdinalNumber(11)).toEqual("11th");
41+
});
42+
test("should return '12th' for 12", () => {
43+
expect(getOrdinalNumber(12)).toEqual("12th");
44+
});
45+
test("should return '13th' for 13", () => {
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
});
1048

11-
test("should return '1st' for 1", () => {
12-
expect(getOrdinalNumber(1)).toEqual("1st");
49+
// Case 8: Identify general 'th' endings
50+
// When the number does not end in 1, 2, or 3 (except teens),
51+
// Then the function should return the 'th' suffix
52+
test("should return '20th' for 20", () => {
53+
expect(getOrdinalNumber(20)).toEqual("20th");
54+
});
55+
test("should return '100th' for 100", () => {
56+
expect(getOrdinalNumber(100)).toEqual("100th");
1357
});

0 commit comments

Comments
 (0)