Skip to content

Commit 1379d50

Browse files
committed
add get ordinal number tests and function
1 parent 4699d42 commit 1379d50

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// check if the input is not a number, not an integer, or if it's negative
3+
if (typeof num !== "number" || !Number.isInteger(num) || num < 0) {
4+
throw new Error("Invalid input: number must be a non-negative integer");
5+
}
6+
7+
if (num % 100 >= 11 && num % 100 <= 13) { // check for numbers ending with 11, 12, or 13
8+
return num + "th";
9+
} else if (num % 10 === 1) { // check for numbers ending with 1 (but not 11)
10+
return num + "st";
11+
} else if (num % 10 === 2) { // check for numbers ending with 2 (but not 12)
12+
return num + "nd";
13+
} else if (num % 10 === 3) { // check for numbers ending with 3 (but not 13)
14+
return num + "rd";
15+
}
16+
// For all other numbers, append "th"
17+
return num + "th";
318
}
419

520
module.exports = getOrdinalNumber;

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,68 @@ 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 fall into any of the above categories,
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(5)).toEqual("5th");
55+
expect(getOrdinalNumber(6)).toEqual("6th");
56+
expect(getOrdinalNumber(7)).toEqual("7th");
57+
expect(getOrdinalNumber(8)).toEqual("8th");
58+
expect(getOrdinalNumber(9)).toEqual("9th");
59+
expect(getOrdinalNumber(10)).toEqual("10th");
60+
expect(getOrdinalNumber(127)).toEqual("127th");
61+
});
62+
63+
// Case 6: Negative numbers
64+
// When the number is negative,
65+
// Then the function should throw an error, as negative numbers are not valid inputs.
66+
test("should throw an error for negative numbers", () => {
67+
expect(() => getOrdinalNumber(-1)).toThrow("Invalid input: number must be a non-negative integer");
68+
expect(() => getOrdinalNumber(-22)).toThrow("Invalid input: number must be a non-negative integer");
69+
});
70+
71+
// Case 7: Non-integer numbers
72+
// When the number is not an integer (e.g., a float),
73+
// Then the function should throw an error, as only non-negative integers are valid inputs.
74+
test("should throw an error for non-integer numbers", () => {
75+
expect(() => getOrdinalNumber(1.5)).toThrow("Invalid input: number must be a non-negative integer");
76+
expect(() => getOrdinalNumber(2.7)).toThrow("Invalid input: number must be a non-negative integer");
77+
});
78+
79+
// Case 8: Non-number inputs
80+
// When the input is not a number (e.g., a string, object, or array),
81+
// Then the function should throw an error, as only non-negative integers are valid inputs.
82+
test("should throw an error for non-number inputs", () => {
83+
expect(() => getOrdinalNumber("1")).toThrow("Invalid input: number must be a non-negative integer");
84+
expect(() => getOrdinalNumber("abcd")).toThrow("Invalid input: number must be a non-negative integer");
85+
});

0 commit comments

Comments
 (0)