Skip to content
11 changes: 9 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
function countChar(str, char) {
return str.split(char).length - 1;
}

module.exports = countChar;

console.log(
countChar(
"The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.",
"a"
)
);
8 changes: 7 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const countChar = require("./count");
// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:
// Then it should: Return how many time the char occurs in the str.

// Scenario: Multiple Occurrences
// Given the input string str,
Expand All @@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => {
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 when character does not exist in the string", () => {
const str = "hello world";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
39 changes: 38 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
function getOrdinalNumber(num) {
return "1st";
num = num.toString();
if (
num.slice(-2) === "11" ||
num.slice(-2) === "12" ||
num.slice(-2) === "13"
) {
return num + "th";
} else if (num.slice(-1) === "1") {
return num + "st";
return num + "st";
} else if (num.slice(-1) === "2") {
return num + "nd";
} else if (num.slice(-1) === "3") {
return num + "rd";
} else {
return num + "th";
}
}
Comment on lines 2 to 20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • There is an unreachable statement (dead code).

  • Note: because of the return statements, using else is optional.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjyuan, Found it 😅😅😅


module.exports = getOrdinalNumber;

console.log(getOrdinalNumber(1));
console.log(getOrdinalNumber(2));
console.log(getOrdinalNumber(3));
console.log(getOrdinalNumber(4));
console.log(getOrdinalNumber(5));
console.log(getOrdinalNumber(6));
console.log(getOrdinalNumber(7));
console.log(getOrdinalNumber(8));
console.log(getOrdinalNumber(9));
console.log(getOrdinalNumber(10));
console.log(getOrdinalNumber(11));
console.log(getOrdinalNumber(12));
console.log(getOrdinalNumber(13));
console.log(getOrdinalNumber(21));
console.log(getOrdinalNumber(22));
console.log(getOrdinalNumber(23));
console.log(getOrdinalNumber(101));
console.log(getOrdinalNumber(111));
console.log(getOrdinalNumber(112));
console.log(getOrdinalNumber(113));
80 changes: 80 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure thorough testing, we need broad scenario coverage. Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories. Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.

For example, we can prepare a test for numbers 2, 22, 132, etc. as

test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
    expect( getOrdinalNumber(2) ).toEqual("2nd");
    expect( getOrdinalNumber(22) ).toEqual("22nd");
    expect( getOrdinalNumber(132) ).toEqual("132nd");
});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjyuan, Done 🫡

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,83 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"

test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});

// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

// Case 4: Identify the ordinal number for 4
// When the number is 4,
// Then the function should return "4th"

test("should return '4th' for 4", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
});

// Case 5: Identify the ordinal number for 11
// When the number is 11,
// Then the function should return "11th"

test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});

// Case 6: Identify the ordinal number for 12
// When the number is 12,
// Then the function should return "12th"

test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
});

// Case 7: Identify the ordinal number for 13
// When the number is 13,
// Then the function should return "13th"

test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 8: Identify the ordinal number for 21
// When the number is 21,
// Then the function should return "21st"

test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});

// Case 9: Identify the ordinal number for 22
// When the number is 22,
// Then the function should return "22nd"

test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
});

// Case 10: Identify the ordinal number for 23
// When the number is 23,
// Then the function should return "23rd"

test("should return '23rd' for 23", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
});

// Case 11: Identify the ordinal number for 101
// When the number is 101,
// Then the function should return "101st"

test("should return '101st' for 101", () => {
expect(getOrdinalNumber(101)).toEqual("101st");
});
13 changes: 12 additions & 1 deletion Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function repeat() {
return "hellohellohello";
const str = arguments[0];
const count = arguments[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just declare them as parameters?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjyuan , Okay 👌

if (count < 0) {
throw new Error("Count must be a non-negative integer");
}
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}
return result;
}

module.exports = repeat;

console.log(repeat("hello", 3));
21 changes: 20 additions & 1 deletion Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const repeat = require("./repeat");
// Given a target string str and a positive integer count,
// When the repeat function is called with these inputs,
// Then it should:
// Then it should: repeat the str count times and return a new string containing the repeated str values.

// case: repeat String:
// Given a target string str and a positive integer count,
Expand All @@ -20,13 +20,32 @@ test("should repeat the string count times", () => {
// Given a target string str and a count equal to 1,
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
test("should return the original string when count is 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hello");
});

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});

// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
test("should throw an error when count is negative", () => {
const str = "hello";
const count = -1;
expect(() => repeat(str, count)).toThrow(
"Count must be a non-negative integer"
);
});