Skip to content

London | 26-ITP-Sep | Maharit Belai | Sprint 2| Practice TDD - #1729

Open
MaharitB wants to merge 51 commits into
CodeYourFuture:mainfrom
MaharitB:coursework/sprint-3-practice-tdd
Open

MaharitB wants to merge 51 commits into
CodeYourFuture:mainfrom
MaharitB:coursework/sprint-3-practice-tdd

Conversation

@MaharitB

Copy link
Copy Markdown

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

Task code

CYF-1060

Changelist

I practiced writing a code based on Test driven development.

Questions

@MaharitB MaharitB added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 17, 2026
@mayowaolokun mayowaolokun added Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 18, 2026

@mayowaolokun mayowaolokun left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for your work on this; it’s clear you’ve put real effort into the exercises. Replacing the hardcoded return values with working implementations is exactly what TDD is about, and it’s great to see you testing a wide range of inputs, including digits, symbols, spaces and error cases.
I’ve left a few comments to help with the next step.
You’re making good progress. Keep up the good work!

Comment thread practice-tdd/count.js
return 5
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] == findCharacter[0]) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comparing each character to findCharacter[0] works well when the target is a single character, as the exercise describes. It might be interesting to try passing something like "an" and see what happens. You could then decide whether the function should handle that case or whether it’s outside what the function promises, and add a test that documents your decision. A test for an empty string could also be a helpful way to confirm the behaviour you expect.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for addressing this. Adding the length check makes the function’s contract clear, and throwing an error for invalid input is a good decision.

Comment thread practice-tdd/count.test.js Outdated
Comment on lines +15 to +20
test(" Should count multiple occurences of character", () => {
const str = "aaaaa";
const char = "c";
const count = countChar(str, char);
expect(count).toEqual(0);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Several of the test names are identical, and a few don’t quite match what the test checks. For example, line 15 checks for zero occurrences, while lines 22 and 28 check for one. Since the name is the first clue when a test fails, it might help to give each one a name that describes its exact scenario.

@@ -1,14 +1,7 @@
// implement a function countChar that counts the number of times a character occurs in a string
import { countChar } from "./count.js";
// Given a string `str` and a single character `char` to search for,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The Given/When/Then comments you removed describe each scenario in plain language, so consider restoring them and writing your own for the new scenarios you’ve added.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the updates. The Given/When/Then comments still appear to have been removed. Restoring them would help document each scenario, so it might be worth adding them back before this is merged.

const char = "a";
const count = countChar(str, char);
expect(count).toEqual(2);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can consider adding tests for an empty string and for a target longer than one character (like "an"), then decide whether the function should handle that or if it’s outside what the function promises.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for adding both tests; this addresses the comment nicely.

});
test("should count multiple occurences of character", () => {
const str = "AAAa";
const char = "a";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great idea to include this test. It shows that only the lowercase “a” is counted and the uppercase “A”s are ignored, so your function is case-sensitive. It might be helpful to rename the test to reflect that, perhaps by mentioning uppercase and lowercase.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for renaming this; it’s much clearer. You could make it even more precise by saying that uppercase letters are not counted, so a reader knows the function is case-sensitive.

Comment thread practice-tdd/repeat-str.test.js Outdated
Comment on lines +35 to +37
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual(" ");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The original comment for this case mentioned that the function should return an empty string when count is 0. Could you take another look at what this test expects? It’s worth checking whether it matches the specification or the current behaviour of your code.

Comment on lines -3 to -10
// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:

// Case: handle multiple repetitions:
// Given a target string `str` and a positive integer `count` greater than 1,
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The Given/When/Then comments you removed are the specifications for each case, so consider restoring them.

Comment thread practice-tdd/repeat-str.test.js Outdated
Comment on lines +10 to +21
test("should repeat the string count times", () => {
const str = "hello";
const count = 2;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohello");
});
test("should repeat the string count times", () => {
const str = "hello";
const count = 4;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohellohello");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All the tests currently share the name “should repeat the string count times”, which could make it hard to tell which one failed. It might help to name each test after the case it covers, for example, a count of 1, a count of 0 or a negative count.

Comment thread practice-tdd/repeat-str.test.js Outdated
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can remove the extra blank line here


test("should repeat the string count times", () => {
expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It would be worth thinking about what should happen when str is an empty string. You might consider adding a test for that case, too.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for adding this test. One thing to check: the input here is a single space " " rather than an empty string "". Please see my comment on line 9 of repeat-str.js, as the two are linked.

@mayowaolokun mayowaolokun added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Oct 19, 2026
@MaharitB MaharitB 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 Oct 20, 2026
@mayowaolokun mayowaolokun added Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 25, 2026

@mayowaolokun mayowaolokun left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for the updates, this is great progress. The main issues from the first review have been addressed: the 111, 112 and 113 cases now work, the zero case in repeatStr returns an empty string, and the test names are much clearer.

I’ve left a few smaller comments.
You’re very close, so keep up the good work!

// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.

test(" Should count 0", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

“Should count 0” is clearer than before. It might help to describe the scenario, for example, that the character doesn’t appear in the string. There’s also still a space at the start of the name.

const count = countChar(str, char);
expect(count).toEqual(1);
});
test("should count multiple occurences of character empty space", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

“Empty space” could be confused with an empty string. Would “space character” describe this test more clearly?

const count = countChar(str, char);
expect(count).toEqual(2);
});
test("should count multiple occurences of character in an empty string", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice addition. Since this test expects 0, it might be worth removing “multiple occurrences” from the name.

const count = countChar(str, char);
expect(count).toEqual(0);
});
test("should count multiple occurences of character in a sentence", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This test seems to share its name with the one on line 52, so it might be worth giving it a unique name that describes its scenario.

throw new Error("Invalid Input");
}

if (numStr.slice(-1) == "1" && numStr.slice(-2) != 11) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

numStr.slice(-2) gives you a string, but it’s compared with the number 11. This currently works because != converts types before comparing. What would happen if you switched to !==, as suggested earlier? It might be worth comparing like with like so the code doesn’t depend on type conversion. Applicable to lines 11 and 14.

count = Math.floor(count);

if (str === " ") {
throw new Error("String shoild not be empty");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Small typo: “shoild” should be “should”.


test("should repeat the string count times", () => {
expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for adding this test. One thing to check: the input here is a single space " " rather than an empty string "". Please see my comment on line 9 of repeat-str.js, as the two are linked.

expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid");
});
test(`should throw an error when string is empty`, () => {
expect(() => repeatStr(" ", 1)).toThrow("String shoild not be empty");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The test name says “empty string”, but the input is a space. Once you’ve decided whether an empty string or a space should be an error, it might be worth updating this test to match. Also, if you fix the “shoild” typo in the error message, remember to update it here too.

Comment on lines -18 to -23

// Case: handle count of 1:
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The Given/When/Then comments for the count of 1 and count of 0 cases are still removed. It might be worth restoring them, as you’ve done for the other cases.

expect(repeatedStr).toEqual("");
});
//case: Handle decimal count:
test("should repeat the stringthe whole number in the count times", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There’s a missing space in “stringthe”. It might also help to make the name clearer, for example, by saying that a decimal count is rounded down.

@mayowaolokun mayowaolokun added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants