Skip to content

Commit 0243e17

Browse files
committed
Complete Practice TDD exercises
1 parent 4b793d9 commit 0243e17

6 files changed

Lines changed: 91 additions & 23 deletions

File tree

‎practice-tdd/count.js‎

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

‎practice-tdd/count.test.js‎

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

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.
20+
test("should return 0 when the character does not exist", () => {
21+
const str = "hello";
22+
const char = "z";
23+
24+
const count = countChar(str, char);
25+
26+
expect(count).toEqual(0);
27+
});
28+
test("should count a single occurrence", () => {
29+
expect(countChar("hello", "h")).toEqual(1);
30+
});
31+
test("should return 0 for an empty string", () => {
32+
expect(countChar("", "a")).toEqual(0);
33+
});

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
export function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return `${num}th`;
6+
}
7+
8+
const lastDigit = num % 10;
9+
10+
if (lastDigit === 1) {
11+
return `${num}st`;
12+
}
13+
14+
if (lastDigit === 2) {
15+
return `${num}nd`;
16+
}
17+
18+
if (lastDigit === 3) {
19+
return `${num}rd`;
20+
}
21+
22+
return `${num}th`;
323
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,28 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
test("should append 'nd' for numbers ending in 2, except those ending in 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(112)).toEqual("112th");
26+
});
27+
28+
test("should append 'rd' for numbers ending in 3, except those ending in 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(23)).toEqual("23rd");
31+
expect(getOrdinalNumber(113)).toEqual("113th");
32+
});
33+
34+
test("should append 'th' for other numbers", () => {
35+
expect(getOrdinalNumber(4)).toEqual("4th");
36+
expect(getOrdinalNumber(5)).toEqual("5th");
37+
expect(getOrdinalNumber(10)).toEqual("10th");
38+
expect(getOrdinalNumber(20)).toEqual("20th");
39+
});
40+
41+
test("should append 'th' for numbers ending in 11, 12 and 13", () => {
42+
expect(getOrdinalNumber(11)).toEqual("11th");
43+
expect(getOrdinalNumber(12)).toEqual("12th");
44+
expect(getOrdinalNumber(13)).toEqual("13th");
45+
});

‎practice-tdd/repeat-str.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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";
1+
export 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
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@ test("should repeat the string count times", () => {
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

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.
19+
test("should return the original string when count is 1", () => {
20+
const str = "hello";
21+
const count = 1;
2322

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.
23+
const repeatedStr = repeatStr(str, count);
2824

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.
25+
expect(repeatedStr).toEqual("hello");
26+
});
27+
test("should return an empty string when count is 0", () => {
28+
const str = "hello";
29+
const count = 0;
30+
31+
const repeatedStr = repeatStr(str, count);
32+
33+
expect(repeatedStr).toEqual("");
34+
});
35+
36+
test("should throw an error when count is negative", () => {
37+
expect(() => repeatStr("hello", -1)).toThrow();
38+
});

0 commit comments

Comments
 (0)