Skip to content

Commit f961fe6

Browse files
Completed Sprint-3 Practice TDD
1 parent b31a586 commit f961fe6

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return stringOfCharacters.split(findCharacter).length - 1;
33
}
44

55
module.exports = countChar;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20+
test("should count zero occurrences of a character", () => {
21+
const str = "aaaaa";
22+
const char = "b";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(0);
25+
});
26+
2027
// Scenario: No Occurrences
2128
// Given the input string `str`,
2229
// And a character `char` that does not exist within `str`.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (num % 10 === 1 && num % 100 !== 11) {
3+
return num + "st";
4+
}
35
}
46

57
module.exports = getOrdinalNumber;
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Implement a function repeatStr
2-
const repeatStr = require("./repeat-str");
2+
33
// Given a target string `str` and a positive integer `count`,
44
// When the repeatStr function is called with these inputs,
55
// Then it should:
@@ -9,24 +9,47 @@ const repeatStr = require("./repeat-str");
99
// 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

12+
function repeatStr(str, count) {
13+
if (count < 0) {
14+
throw new Error("Count must be a non-negative integer");
15+
} else if (count === 0) {
16+
return "";
17+
} else {
18+
return str.repeat(count);
19+
}
20+
}
21+
1222
test("should repeat the string count times", () => {
1323
const str = "hello";
1424
const count = 3;
1525
const repeatedStr = repeatStr(str, count);
1626
expect(repeatedStr).toEqual("hellohellohello");
1727
});
1828

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.
29+
test("should return the original string when count is 1", () => {
30+
const str = "hello";
31+
const count = 1;
32+
const repeatedStr = repeatStr(str, count);
33+
expect(repeatedStr).toEqual("hello");
34+
});
35+
2336

2437
// Case: Handle count of 0:
2538
// Given a target string `str` and a `count` equal to 0,
2639
// When the repeatStr function is called with these inputs,
2740
// Then it should return an empty string.
41+
test("should return an empty string when count is 0", () => {
42+
const str = "hello";
43+
const count = 0;
44+
const repeatedStr = repeatStr(str, count);
45+
expect(repeatedStr).toEqual("");
46+
});
2847

2948
// Case: Handle negative count:
3049
// Given a target string `str` and a negative integer `count`,
3150
// When the repeatStr function is called with these inputs,
3251
// Then it should throw an error, as negative counts are not valid.
52+
test("should throw an error for a negative count", () => {
53+
expect(() => repeatStr("hello", -1)).toThrow();
54+
55+
});

0 commit comments

Comments
 (0)