Skip to content

Commit 1cdc2ea

Browse files
committed
Implement repeatStr function and add Jest tests
- Implement repeatStr to repeat a string a given number of times - Handles count = 0, count = 1, count > 1 - Throws error for negative counts - Add Jest tests covering: - multiple repetitions - single repetition - zero repetitions - negative counts
1 parent 3372770 commit 1cdc2ea

File tree

6 files changed

+95
-6
lines changed

6 files changed

+95
-6
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
4+
for (let i = 0; i < stringOfCharacters.length; i++) {
5+
if (stringOfCharacters[i] === findCharacter) {
6+
count++;
7+
}
8+
}
9+
10+
return count;
311
}
412

513
module.exports = countChar;

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const countChar = require("./count");
99
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
1010
// When the function is called with these inputs,
1111
// Then it should correctly count occurrences of `char`.
12-
1312
test("should count multiple occurrences of a character", () => {
1413
const str = "aaaaa";
1514
const char = "a";
@@ -22,3 +21,34 @@ test("should count multiple occurrences of a character", () => {
2221
// And a character `char` that does not exist within `str`.
2322
// When the function is called with these inputs,
2423
// Then it should return 0, indicating that no occurrences of `char` were found.
24+
test("should return 0 when character does not occur in string", () => {
25+
const str = "hello";
26+
const char = "z";
27+
const count = countChar(str, char);
28+
expect(count).toEqual(0);
29+
});
30+
31+
// Scenario: Empty String
32+
// Given an empty string `str`,
33+
// And any character `char`,
34+
// When the function is called with these inputs,
35+
// Then it should return 0, because no characters exist in the string.
36+
test("should return 0 when string is empty", () => {
37+
const str = "";
38+
const char = "a";
39+
const count = countChar(str, char);
40+
expect(count).toEqual(0);
41+
});
42+
43+
// Scenario: Single Occurrence
44+
// Given the input string `str`,
45+
// And a character `char` that occurs exactly once within `str`.
46+
// When the function is called with these inputs,
47+
// Then it should return 1, indicating a single occurrence of `char`.
48+
test("should return 1 when character occurs once", () => {
49+
const str = "hello";
50+
const char = "h";
51+
const count = countChar(str, char);
52+
expect(count).toEqual(1);
53+
});
54+
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
1+
function getOrdinalNumber(n) {
2+
const j = n % 10,
3+
k = n % 100;
4+
if (k >= 11 && k <= 13) return n + "th";
5+
if (j === 1) return n + "st";
6+
if (j === 2) return n + "nd";
7+
if (j === 3) return n + "rd";
8+
return n + "th";
39
}
410

511
module.exports = getOrdinalNumber;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,26 @@ 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+
// Ends with 2, except 12
23+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(132)).toEqual("132nd");
27+
});
28+
29+
// Ends with 3, except 13
30+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
31+
expect(getOrdinalNumber(3)).toEqual("3rd");
32+
expect(getOrdinalNumber(23)).toEqual("23rd");
33+
expect(getOrdinalNumber(133)).toEqual("133rd");
34+
});
35+
36+
// All others
37+
test("should append 'th' for all other numbers", () => {
38+
expect(getOrdinalNumber(4)).toEqual("4th");
39+
expect(getOrdinalNumber(11)).toEqual("11th");
40+
expect(getOrdinalNumber(12)).toEqual("12th");
41+
expect(getOrdinalNumber(13)).toEqual("13th");
42+
expect(getOrdinalNumber(14)).toEqual("14th");
43+
});
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) throw new Error("Count must be a non-negative integer");
3+
return str.repeat(count);
34
}
45

56
module.exports = repeatStr;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,34 @@ 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 string itself when count is 1", () => {
24+
const str = "world";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("world");
28+
});
29+
2330

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.
35+
test("should return an empty string when count is 0", () => {
36+
const str = "hello";
37+
const count = 0;
38+
const repeatedStr = repeatStr(str, count);
39+
expect(repeatedStr).toEqual("");
40+
});
2841

2942
// Case: Handle negative count:
3043
// Given a target string `str` and a negative integer `count`,
3144
// When the repeatStr function is called with these inputs,
3245
// Then it should throw an error, as negative counts are not valid.
46+
test("should throw an error for negative count", () => {
47+
const str = "oops";
48+
const count = -3;
49+
50+
expect(() => {
51+
repeatStr(str, count);
52+
}).toThrow("Count must be a non-negative integer");
53+
});

0 commit comments

Comments
 (0)