Skip to content

Commit 1cce0d0

Browse files
committed
Implement getOrdinalNumber and add Jest tests usingtdd
1 parent f77b982 commit 1cce0d0

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1+
/**
2+
* Converts a number into its ordinal string form (e.g. 1 → "1st", 2 → "2nd").
3+
*
4+
* English ordinal rules:
5+
* - Numbers ending in 11, 12, or 13 always use "th" (they are exceptions).
6+
* - Numbers ending in 1 (but not 11) use "st".
7+
* - Numbers ending in 2 (but not 12) use "nd".
8+
* - Numbers ending in 3 (but not 13) use "rd".
9+
* - Everything else uses "th".
10+
*
11+
* @param {number} num - A positive whole number (e.g. 1, 22, 113).
12+
* @returns {string} The number with its ordinal suffix appended (e.g. "1st", "22nd").
13+
*
14+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder
15+
* @see https://www.freecodecamp.org/news/javascript-modulo-operator/
16+
*/
117
function getOrdinalNumber(num) {
2-
return "1st";
18+
// % is the "remainder" operator (also called modulo).
19+
// num % 100 gives the last two digits of any number.
20+
// e.g. 113 % 100 = 13, so we catch the 11/12/13 exceptions correctly.
21+
const lastTwoDigits = num % 100;
22+
23+
// num % 10 gives the last single digit of any number.
24+
// e.g. 21 % 10 = 1, so we know it ends in 1 → "st".
25+
const lastDigit = num % 10;
26+
27+
// Check the 11/12/13 exceptions FIRST.
28+
// If we checked lastDigit first, 11 would wrongly get "st" instead of "th".
29+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
30+
return num + "th";
31+
}
32+
33+
if (lastDigit === 1) return num + "st";
34+
if (lastDigit === 2) return num + "nd";
35+
if (lastDigit === 3) return num + "rd";
36+
37+
// All remaining numbers (4–9, 0, and any not caught above) use "th".
38+
return num + "th";
339
}
440

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

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,43 @@ 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, or 13
41+
// When the number ends with 11, 12, or 13,
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, or 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
});
48+
49+
// Case 5: All other numbers
50+
// When the number does not end with 1, 2, or 3 (and is not an exception),
51+
// Then the function should return a string by appending "th" to the number.
52+
test("should append 'th' for all other numbers", () => {
53+
expect(getOrdinalNumber(4)).toEqual("4th");
54+
expect(getOrdinalNumber(10)).toEqual("10th");
55+
expect(getOrdinalNumber(20)).toEqual("20th");
56+
expect(getOrdinalNumber(100)).toEqual("100th");
57+
expect(getOrdinalNumber(111)).toEqual("111th");
58+
});
59+
60+

0 commit comments

Comments
 (0)