Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project-CLI-Treasure-Hunt
Submodule Project-CLI-Treasure-Hunt added at 6668c4
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
}
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}
console.log(countChar("hello world!", "l"));

module.exports = countChar;
7 changes: 7 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ 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);
});
10 changes: 9 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,13 @@
function getOrdinalNumber(num) {
return "1st";
const suffixes = ["th", "st", "nd", "rd"];
const v = num % 100;
console.log(v);
Copy link
Contributor

Choose a reason for hiding this comment

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

  • It's best practice to give variables meaningful names. Can you think of a more meaningful name to replace v?
  • Can you keep the code clean by removing all debugging code?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @cjyuan. I updated the V variable to date to make it more meaningful. also removed the console.logs to make it more clean.

if (v >= 11 && v <= 13) {
return num + "th";
}
const suffix = suffixes[(v % 10)] || "th";
return num + suffix;
}
console.log(getOrdinalNumber(52));

module.exports = getOrdinalNumber;
14 changes: 14 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

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

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});
test("should return '24th' for 24", () => {
expect(getOrdinalNumber(24)).toEqual("24th");
});
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});
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 scenarios that cover all possible cases.
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");
});

Can you update the tests in this script to make it comprehensive so that the test categories can cover all valid numbers?

15 changes: 13 additions & 2 deletions 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";
function repeat(str , count) {
if (count < 0) {
return "Count must be a non-negative integer";
}
Comment on lines 2 to 4
Copy link
Contributor

Choose a reason for hiding this comment

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

How would the caller distinguish the result of the following two function calls?

  1. repeat("Count must be a non-negative integer", 1)
  2. repeat("", -1)

Both function calls return the same value.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I see your point. I changed it so it throw error.

if (count === 0) {
return "";
}
for (let i = 0; i < count; i++) {
return str.repeat(count);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This loop will always iterate exactly once. Do you know why?

Copy link
Author

Choose a reason for hiding this comment

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

Good point. That's because return is inside the loop. so I changed it.

}
console.log(repeat("hello", -2));



module.exports = repeat;
24 changes: 18 additions & 6 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Implement a function repeat
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:

// case: repeat String:
// Given a target string str and a positive integer count,
// When the repeat function is called with these inputs,
Expand All @@ -20,13 +16,29 @@ 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 = "world";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("world");
});
// 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 = "test";
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 return a message for negative count", () => {
const str = "error";
const count = -2;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("Count must be a non-negative integer");
});
Copy link
Contributor

Choose a reason for hiding this comment

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

If you modified repeat() to throw an error when count is negative, and you wanted to test if the function can throw an error as expected, you can use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for bring this up. I changed it so it will throw error.

Loading