Skip to content

Commit d370f0b

Browse files
committed
Complete Sprint 3 Practice TDD
1 parent 29ef74a commit d370f0b

6 files changed

Lines changed: 109 additions & 16 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: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,57 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
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:
63

74
// 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-
5+
// Given a string `str` and a character `char` that appears several times,
6+
// When the function is called,
7+
// Then it should correctly count each occurrence.
138
test("should count multiple occurrences of a character", () => {
149
const str = "aaaaa";
1510
const char = "a";
1611
const count = countChar(str, char);
1712
expect(count).toEqual(5);
1813
});
1914

15+
// Scenario: Single Occurrence
16+
// Given a string containing exactly one match,
17+
// When the function is called,
18+
// Then it should return 1.
19+
test("should count a single occurrence of a character", () => {
20+
const str = "banana";
21+
const char = "n";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(2);
24+
});
25+
2026
// 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.
27+
// Given a string with no matching character,
28+
// When the function is called,
29+
// Then it should return 0.
30+
test("should return 0 when the character does not occur", () => {
31+
const str = "hello";
32+
const char = "z";
33+
const count = countChar(str, char);
34+
expect(count).toEqual(0);
35+
});
36+
37+
// Edge case: Empty String
38+
// Given an empty string,
39+
// When the function is called with any character,
40+
// Then it should return 0.
41+
test("should return 0 for an empty string", () => {
42+
const str = "";
43+
const char = "a";
44+
const count = countChar(str, char);
45+
expect(count).toEqual(0);
46+
});
47+
48+
// Edge case: Character at start and end
49+
// Given a string where the character appears at both ends,
50+
// When the function is called,
51+
// Then it should count both occurrences.
52+
test("should count characters at the start and end of the string", () => {
53+
const str = "ahelloa";
54+
const char = "a";
55+
const count = countChar(str, char);
56+
expect(count).toEqual(2);
57+
});

‎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 absNum = Math.abs(num);
3+
const lastTwoDigits = absNum % 100;
4+
const lastDigit = absNum % 10;
5+
6+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
7+
return `${num}th`;
8+
}
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ 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 with 2, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(102)).toEqual("102nd");
26+
});
27+
28+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(23)).toEqual("23rd");
31+
expect(getOrdinalNumber(103)).toEqual("103rd");
32+
});
33+
34+
test("should append 'th' for all other numbers, including 11, 12 and 13", () => {
35+
expect(getOrdinalNumber(4)).toEqual("4th");
36+
expect(getOrdinalNumber(11)).toEqual("11th");
37+
expect(getOrdinalNumber(12)).toEqual("12th");
38+
expect(getOrdinalNumber(13)).toEqual("13th");
39+
expect(getOrdinalNumber(111)).toEqual("111th");
40+
});

‎practice-tdd/repeat-str.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
export function repeatStr() {
1+
export function repeatStr(str, count) {
22
// 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).
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
5+
if (count < 0) {
6+
throw new Error("count must be non-negative");
7+
}
8+
9+
let result = "";
10+
11+
for (let i = 0; i < count; i++) {
12+
result += str;
13+
}
14+
15+
return result;
516
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should return the original string when count is 1", () => {
24+
expect(repeatStr("hello", 1)).toEqual("hello");
25+
});
2326

2427
// Case: Handle count of 0:
2528
// Given a target string `str` and a `count` equal to 0,
2629
// When the repeatStr function is called with these inputs,
2730
// Then it should return an empty string.
31+
test("should return an empty string when count is 0", () => {
32+
expect(repeatStr("hello", 0)).toEqual("");
33+
});
2834

2935
// Case: Handle negative count:
3036
// Given a target string `str` and a negative integer `count`,
3137
// When the repeatStr function is called with these inputs,
3238
// Then it should throw an error, as negative counts are not valid.
39+
test("should throw an error for negative count", () => {
40+
expect(() => repeatStr("hello", -1)).toThrow("count must be non-negative");
41+
});

0 commit comments

Comments
 (0)