Skip to content

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

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

MaharitB wants to merge 38 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.

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.

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.

});
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.

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 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.

// 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.

@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
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