Skip to content

Commit 714d1bf

Browse files
committed
Complete Practice TDD exercises
1 parent 3fb1336 commit 714d1bf

6 files changed

Lines changed: 87 additions & 32 deletions

File tree

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 (const character of stringOfCharacters) {
5+
if (character === 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
test("should return 0 when the character does not occur", () => {
26+
const str = "hello";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
function getOrdinalNumber(num) {
2-
return `${num}st`;
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return `${num}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

5-
module.exports = getOrdinalNumber;
24+
module.exports = getOrdinalNumber;
Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
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.
122

133
// 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.
164
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
175
expect(getOrdinalNumber(1)).toEqual("1st");
186
expect(getOrdinalNumber(21)).toEqual("21st");
197
expect(getOrdinalNumber(131)).toEqual("131st");
208
});
9+
10+
// Case 2: Numbers ending with 2 (but not 12)
11+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
12+
expect(getOrdinalNumber(2)).toEqual("2nd");
13+
expect(getOrdinalNumber(22)).toEqual("22nd");
14+
expect(getOrdinalNumber(132)).toEqual("132nd");
15+
});
16+
17+
// Case 3: Numbers ending with 3 (but not 13)
18+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
19+
expect(getOrdinalNumber(3)).toEqual("3rd");
20+
expect(getOrdinalNumber(23)).toEqual("23rd");
21+
expect(getOrdinalNumber(133)).toEqual("133rd");
22+
});
23+
24+
// Case 4: All other numbers
25+
test("should append 'th' for all other numbers", () => {
26+
expect(getOrdinalNumber(4)).toEqual("4th");
27+
expect(getOrdinalNumber(11)).toEqual("11th");
28+
expect(getOrdinalNumber(12)).toEqual("12th");
29+
expect(getOrdinalNumber(13)).toEqual("13th");
30+
expect(getOrdinalNumber(20)).toEqual("20th");
31+
});
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
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";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
let result = "";
7+
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
12+
return result;
513
}
614

715
module.exports = repeatStr;
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
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

78
// 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-
129
test("should repeat the string count times", () => {
1310
const str = "hello";
1411
const count = 3;
@@ -17,16 +14,22 @@ test("should repeat the string count times", () => {
1714
});
1815

1916
// 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.
17+
test("should return the original string when count is 1", () => {
18+
const str = "hello";
19+
const count = 1;
20+
const repeatedStr = repeatStr(str, count);
21+
expect(repeatedStr).toEqual("hello");
22+
});
2323

2424
// 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.
25+
test("should return an empty string when count is 0", () => {
26+
const str = "hello";
27+
const count = 0;
28+
const repeatedStr = repeatStr(str, count);
29+
expect(repeatedStr).toEqual("");
30+
});
2831

2932
// 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.
33+
test("should throw an error when count is negative", () => {
34+
expect(() => repeatStr("hello", -1)).toThrow();
35+
});

0 commit comments

Comments
 (0)