Skip to content

Commit 5211d13

Browse files
committed
Adjustments made on the repeat-str.js and repeat-str.test.js files to implement the repeatStr function and add test cases for it. The repeatStr function now uses the built-in repeat method to repeat the input string a specified number of times. The test cases cover various scenarios, including normal repetition, handling of zero count, and handling of negative count.
1 parent 26e3e7d commit 5211d13

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
test("should return 0 when the character does not occur in the string", () => {
26+
const str = "hello world";
27+
const char = "x";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
} );
31+

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,28 @@ function getOrdinalNumber(num) {
33
}
44

55
module.exports = getOrdinalNumber;
6+
// Below is the implementation of the getOrdinalNumber function that converts a number to its ordinal form (e.g., 1 to "1st", 2 to "2nd", etc.).
7+
// The function takes a single parameter `num`, which is the number to convert.
8+
// It checks the last digit of the number to determine the appropriate suffix ("st", "nd", "rd", or "th") and returns the number concatenated with its ordinal suffix as a string.
9+
10+
function getOrdinalNumber(num) {
11+
const lastDigit = num % 10;
12+
const lastTwoDigits = num % 100;
13+
14+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
15+
return num + "th";
16+
}
17+
18+
switch (lastDigit) {
19+
case 1:
20+
return num + "st";
21+
case 2:
22+
return num + "nd";
23+
case 3:
24+
return num + "rd";
25+
default:
26+
return num + "th";
27+
}
28+
}
29+
30+
module.exports = getOrdinalNumber;

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

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ function repeatStr() {
33
}
44

55
module.exports = repeatStr;
6+
// Below is the implementation of the repeatStr function that takes a string and a number as parameters and returns the string repeated the specified number of times.
7+
// The function uses the built-in `repeat` method of strings to achieve this. It takes the input string and repeats it `n` times, where `n` is the number provided as the second parameter.
8+
9+
function repeatStr(str, n) {
10+
return str.repeat(n);
11+
}
12+
13+
module.exports = repeatStr;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,30 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should return the original string when count is 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should return an empty string when count is 0", () => {
35+
const str = "hello";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
test("should throw an error when count is negative", () => {
46+
const str = "hello";
47+
const count = -1;
48+
expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer");
49+
});

0 commit comments

Comments
 (0)