Skip to content

Conversation

@AhmadHmedann
Copy link

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

I’ve implemented all the required functions along with comprehensive Jest tests that aim to cover all possible cases. I’m open to any feedback that could help me improve my skills.

Questions

There are two questions in the repeat.js file. Thanks in advance.

@AhmadHmedann AhmadHmedann added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 31, 2025
Comment on lines 25 to 43
test("should return 0 occurrences of b characters ", () => {
const str = "Ahmad Hmedan";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should return 1 occurrence of A characters", () => {
const str = "Ahmad Hmedan";
const char = "A";
const count = countChar(str, char);
expect(count).toEqual(1);
});
test("should return 2 occurrence of m characters", () => {
const str = "Ahmad Hmedan";
const char = "m";
const count = countChar(str, char);
expect(count).toEqual(2);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

The test description usually describe a general case, like the one on line 13. And then for each case we can test multiple samples that belong to that case.

A specific description like this "should return 0 occurrences of b characters" could confuse the the person implementing the function into thinking "what is so special about checking 0 occurrences of 'b' in a string?"

Comment on lines 50 to 55
test("should return 1 occurrence of @ characters in empty string", () => {
const str = "Ahmadhm@gamil.com";
const char = "@";
const count = countChar(str, char);
expect(count).toEqual(1);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

  • The test description does not quite match the test.
  • Could consider also generalise this test or combine it with others.

Comment on lines 17 to 52
test("should return '22nd' for 22", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
});
// Case 3: Identify the ordinal number for 73
// When the number is 73,
// Then the function should return 73rd"

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

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

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

// Case 5: Identify the ordinal number for number ends with 11,12, or 13
// When the number is 111,
// Then the function should return 111th"
// When the number is 212,
// Then the function should return 212th"
// When the number is 413,
// Then the function should return 413th"

test("should return '111th' for 111", () => {
expect(getOrdinalNumber(111)).toEqual("111th");
});
test("should return '212th' for 212", () => {
expect(getOrdinalNumber(212)).toEqual("212th");
});
test("should return '413th' for 413", () => {
expect(getOrdinalNumber(413)).toEqual("413th");
});
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");
});

function repeat() {
return "hellohellohello";
function repeat(myString, repeatNumber) {
if (repeatNumber < 0) return "Invalid Input must be a positive number";
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("Invalid Input must be a positive number", 1)
  2. repeat("", -1)

Both function calls return the same value.

Comment on lines +15 to +22
// function repeat(myString, repeatNumber) {
// if (repeatNumber < 0) return "Invalid Input must be a positive number";
// let str = "";
// for (let i = 0; i < repeatNumber; i++) {
// str += myString;
// }
// return str;
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

This works too.

While knowing how to use built-in functions can improve productivity and performance, it is equally important to know how to implement such a function without relying on any built-in function.

Comment on lines 46 to 51
test("should return empty when the count is negative", () => {
const str = "CYF";
const count = -2;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("Invalid Input must be a positive number");
});
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)

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Nov 8, 2025
@AhmadHmedann AhmadHmedann added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Nov 8, 2025
@cjyuan
Copy link
Contributor

cjyuan commented Nov 9, 2025

Changes look good.

I wished I have the super power to interpret what your emoji means.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Nov 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants