Skip to content

Commit 27995e0

Browse files
fix: restore practice-tdd directory to match main
1 parent 1e0d93f commit 27995e0

7 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎practice-tdd/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Practice TDD
2+
3+
In this section you'll practice this key skill of building up your program test first.
4+
5+
Use the Jest syntax and complete the provided files, meeting the acceptance criteria for each function. Use the VSCode test runner to run your tests and check your progress.
6+
7+
Write the tests _before_ the code that will make them pass.
8+
9+
Recommended order:
10+
11+
1. `count.test.js`
12+
1. `repeat-str.test.js`
13+
1. `get-ordinal-number.test.js`

‎practice-tdd/count.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function countChar(stringOfCharacters, findCharacter) {
2+
return 5
3+
}

‎practice-tdd/count.test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// implement a function countChar that counts the number of times a character occurs in a string
2+
import { countChar } from "./count.js";
3+
// Given a string `str` and a single character `char` to search for,
4+
// When the countChar function is called with these inputs,
5+
// Then it should:
6+
7+
// Scenario: Multiple Occurrences
8+
// Given the input string `str`,
9+
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
10+
// When the function is called with these inputs,
11+
// Then it should correctly count occurrences of `char`.
12+
13+
test("should count multiple occurrences of a character", () => {
14+
const str = "aaaaa";
15+
const char = "a";
16+
const count = countChar(str, char);
17+
expect(count).toEqual(5);
18+
});
19+
20+
// Scenario: No Occurrences
21+
// Given the input string `str`,
22+
// And a character `char` that does not exist within `str`.
23+
// When the function is called with these inputs,
24+
// Then it should return 0, indicating that no occurrences of `char` were found.

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getOrdinalNumber(num) {
2+
return "1st";
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getOrdinalNumber } from "./get-ordinal-number.js";
2+
// In this week's prep, we started implementing getOrdinalNumber.
3+
4+
// Continue testing and implementing getOrdinalNumber for additional cases.
5+
// Write your tests using Jest — remember to run your tests often for continual feedback.
6+
7+
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
8+
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
9+
// Instead of writing tests for individual numbers, consider grouping all possible input values
10+
// into meaningful categories. Then, select representative samples from each category to test.
11+
// This approach improves coverage and makes our tests easier to maintain.
12+
13+
// Case 1: Numbers ending with 1 (but not 11)
14+
// When the number ends with 1, except those ending with 11,
15+
// Then the function should return a string by appending "st" to the number.
16+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17+
expect(getOrdinalNumber(1)).toEqual("1st");
18+
expect(getOrdinalNumber(21)).toEqual("21st");
19+
expect(getOrdinalNumber(131)).toEqual("131st");
20+
});

‎practice-tdd/repeat-str.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function repeatStr() {
2+
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
3+
// The goal is to re-implement that function, not to use it.
4+
return "hellohellohello";
5+
}

‎practice-tdd/repeat-str.test.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Implement a function repeatStr
2+
import { repeatStr } from "./repeat-str.js";
3+
// Given a target string `str` and a positive integer `count`,
4+
// When the repeatStr function is called with these inputs,
5+
// Then it should:
6+
7+
// Case: handle multiple repetitions:
8+
// Given a target string `str` and a positive integer `count` greater than 1,
9+
// When the repeatStr function is called with these inputs,
10+
// Then it should return a string that contains the original `str` repeated `count` times.
11+
12+
test("should repeat the string count times", () => {
13+
const str = "hello";
14+
const count = 3;
15+
const repeatedStr = repeatStr(str, count);
16+
expect(repeatedStr).toEqual("hellohellohello");
17+
});
18+
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.
23+
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,
27+
// Then it should return an empty string.
28+
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.

0 commit comments

Comments
 (0)