Skip to content

Commit d961cef

Browse files
committed
Implement getOrdinalNumber function and add comprehensive tests for ordinal suffixes
1 parent c6aee63 commit d961cef

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// 1. Handle the 11, 12, 13 exceptions first
3+
if (num === 11 || num === 12 || num === 13) {
4+
return num + "th";
5+
}
6+
7+
const lastDigit = num % 10;
8+
9+
if (lastDigit === 1) {
10+
return num + "st";
11+
}
12+
13+
if (lastDigit === 2) {
14+
return num + "nd";
15+
}
16+
17+
if (lastDigit === 3) {
18+
return num + "rd";
19+
}
20+
21+
return num + "th";
322
}
423

524
module.exports = getOrdinalNumber;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,39 @@ 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: Numbers ending with 0, 4, 5, 6, 7, 8, or 9 (but not 10)
50+
// When the number ends with 0, 4, 5, 6, 7, 8, or 9, except those ending with 10,
51+
// Then the function should return a string by appending "th" to the number.
52+
test("should append 'th' for numbers ending with 0, 4-9, except those ending with 10", () => {
53+
expect(getOrdinalNumber(4)).toEqual("4th");
54+
expect(getOrdinalNumber(10)).toEqual("10th");
55+
expect(getOrdinalNumber(14)).toEqual("14th");
56+
});
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, num) {
2+
if (num < 0) {
3+
throw new Error("Count must be a non-negative integer");
4+
}
5+
6+
let result = "";
7+
for (let i = 0; i < num; i++) {
8+
result += str;
9+
}
10+
return result;
311
}
412

513
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,34 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original string when count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should return an empty string when count is 0", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should throw an error when count is negative", () => {
49+
const str = "hello";
50+
const count = -1;
51+
expect(() => repeatStr(str, count)).toThrow(
52+
"Count must be a non-negative integer"
53+
);
54+
});

0 commit comments

Comments
 (0)