Skip to content

Commit 2da8e43

Browse files
committed
practice tdd completed
1 parent 3372770 commit 2da8e43

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
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 letter of stringOfCharacters) {
5+
if (letter === 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: 15 additions & 0 deletions
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:
@@ -22,3 +23,17 @@ test("should count multiple occurrences of a character", () => {
2223
// And a character `char` that does not exist within `str`.
2324
// When the function is called with these inputs,
2425
// Then it should return 0, indicating that no occurrences of `char` were found.
26+
27+
test("should count multiple occurrences of a character", () => {
28+
const str = "aaaaa";
29+
const char = "a";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(5);
32+
});
33+
34+
test("should return 0 when the character does not appear in the string", () => {
35+
const str = "hello";
36+
const char = "z";
37+
const count = countChar(str, char);
38+
expect(count).toEqual(0);
39+
});
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+
const lastTwoDigits = num % 100;
3+
const lastDigit = num % 10;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return number + "th";
7+
}
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;
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
return str.repeat(count);
37
}
48

59
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
@@ -21,12 +21,33 @@ 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+
52+
expect(() => repeatStr(str, count)).toThrow();
53+
});

0 commit comments

Comments
 (0)