Skip to content

Commit 51bf74e

Browse files
committed
Complete Sprint 3 practice TDD tasks
1 parent 37c47d1 commit 51bf74e

File tree

6 files changed

+87
-19
lines changed

6 files changed

+87
-19
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
// implement a function countChar that counts the number of times a character occurs in a string
2+
13
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
4+
let count = 0;
5+
6+
for (const character of stringOfCharacters) {
7+
if (character === findCharacter) {
8+
count += 1;
9+
}
10+
}
11+
12+
return count;
313
}
414

515
module.exports = countChar;

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
const countChar = require("./count");
3+
34
// Given a string `str` and a single character `char` to search for,
45
// When the countChar function is called with these inputs,
56
// Then it should:
@@ -14,11 +15,33 @@ test("should count multiple occurrences of a character", () => {
1415
const str = "aaaaa";
1516
const char = "a";
1617
const count = countChar(str, char);
18+
1719
expect(count).toEqual(5);
1820
});
1921

2022
// Scenario: No Occurrences
2123
// Given the input string `str`,
2224
// And a character `char` that does not exist within `str`.
2325
// When the function is called with these inputs,
24-
// Then it should return 0, indicating that no occurrences of `char` were found.
26+
// Then it should return 0.
27+
28+
test("should return 0 when the character does not occur in the string", () => {
29+
const str = "hello";
30+
const char = "z";
31+
const count = countChar(str, char);
32+
33+
expect(count).toEqual(0);
34+
});
35+
36+
// Scenario: Mixed Characters
37+
// Given a string containing different characters
38+
// When searching for a character that appears some of the time
39+
// Then the function should count only matching characters
40+
41+
test("should count matching characters in a mixed string", () => {
42+
const str = "banana";
43+
const char = "a";
44+
const count = countChar(str, char);
45+
46+
expect(count).toEqual(3);
47+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ function getOrdinalNumber(number) {
2323
return `${number}th`;
2424
}
2525

26-
module.exports = getOrdinalNumber;
26+
module.exports = getOrdinalNumber;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ test("should append 'th' for all other numbers", () => {
5555
expect(getOrdinalNumber(10)).toEqual("10th");
5656
expect(getOrdinalNumber(24)).toEqual("24th");
5757
expect(getOrdinalNumber(100)).toEqual("100th");
58-
});
58+
});
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
// Implement a function repeatStr that repeats a string a given number of times
2+
3+
function repeatStr(str, count) {
4+
// Negative numbers are not valid
5+
if (count < 0) {
6+
throw new Error("Count cannot be negative");
7+
}
8+
9+
// Repeat the string count times
10+
let result = "";
11+
12+
for (let i = 0; i < count; i++) {
13+
result += str;
14+
}
15+
16+
return result;
317
}
418

519
module.exports = repeatStr;
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
11
// Implement a function repeatStr
22
const repeatStr = require("./repeat-str");
3+
34
// Given a target string `str` and a positive integer `count`,
45
// When the repeatStr function is called with these inputs,
56
// Then it should:
67

7-
// Case: handle multiple repetitions:
8+
// Case: handle multiple repetitions
89
// Given a target string `str` and a positive integer `count` greater than 1,
9-
// When the repeatStr function is called with these inputs,
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

1212
test("should repeat the string count times", () => {
1313
const str = "hello";
1414
const count = 3;
1515
const repeatedStr = repeatStr(str, count);
16+
1617
expect(repeatedStr).toEqual("hellohellohello");
1718
});
1819

19-
// Case: handle count of 1:
20-
// Given a target string `str` and a `count` equal to 1,
21-
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original `str` without repetition.
20+
// Case: handle count of 1
21+
// Given a target string `str` and a count equal to 1,
22+
// Then it should return the original string.
2323

24-
// Case: Handle count of 0:
25-
// Given a target string `str` and a `count` equal to 0,
26-
// When the repeatStr function is called with these inputs,
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+
29+
expect(repeatedStr).toEqual("hello");
30+
});
31+
32+
// Case: handle count of 0
33+
// Given a target string `str` and a count equal to 0,
2734
// Then it should return an empty string.
2835

29-
// Case: Handle negative count:
30-
// Given a target string `str` and a negative integer `count`,
31-
// When the repeatStr function is called with these inputs,
32-
// Then it should throw an error, as negative counts are not valid.
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+
41+
expect(repeatedStr).toEqual("");
42+
});
43+
44+
// Case: handle negative count
45+
// Given a negative integer count,
46+
// Then the function should throw an error.
47+
48+
test("should throw an error when count is negative", () => {
49+
const str = "hello";
50+
const count = -1;
51+
52+
expect(() => repeatStr(str, count)).toThrow();
53+
});

0 commit comments

Comments
 (0)