Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions practice-tdd/count.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your code is correct. Just wondering what is the advantage of using this approach over other implementations? Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Of course from what I understand, its when its more test driven it helps you build the code as your making tests for it, it helps keep a better flow and streamlined work on the code while addressing the tests at the same time. If you just build the code itself without tests in this way it can lead to unseen issues that end up rebuild large chunks of the code that can result in making more bugs and issues.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry maybe my original question was not clear. I didn't mean the advantages of TDD, I was wondering how would you come up with the following approach with the given test cases:

  • charMatch = str.match(new RegExp(char, "g"))
  • charMatch === null
  • return charMatch.length

Especially what is the advantage of such implementation but not other programming logics? Thank you for your claification.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think it was when I was building up the code, I looked for something to use to count the "char" but i cant recall which i used but was only counting the first letter so when I searched for options on MDN i found .match() and used that one.

I found out about RegExp on the same page as .match() and saw it was used to help with finding the pattern of what i was searching for.

I still need to look into it more to understand it better to be honest.

If i think of it, this takes all the occurrences of g out of the string
For the null one I ran into an error that was showing null instead of 0 at the time so i think I put in an if to catch it out to show 0.

After finding those when I was stumped I couldn't think of any other logics I could have used at the time. I think i did it diffrently before so will look it up after this.

@hackertainment hackertainment Oct 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No worries, although the explanations are not quite accurate, at least I understand your thinking. Even this is not the best implementation, I would let you pass this one because the program runs correctly. Maybe try to book a mentored coding session with volunteer to try another way to solve the same exercise. Thank you.

p.s.: the g in new RegExp(char, "g") means "global search" but not searching for character "g".

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
export function countChar(str, char) {
let charMatch = str.match(new RegExp(char, "g"));
if (charMatch === null) {
return 0;
}
return charMatch.length;
}
42 changes: 42 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,45 @@ test("should count multiple occurrences of a character", () => {
// And a character `char` that does not exist within `str`.
// 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 with no occurrences of a character", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});
Comment on lines +26 to +31

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 is a special str that would give 0 anyway. Please add that test case as well. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Is that when str has nothing in it? I have added a test for this aswell as some others that have just thought of.


test("should count 0 with no empty string", () => {
const str = "";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should count multiple occurrences of a character in random string", () => {
const str = "accababaade";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character in string with empty spaces", () => {
const str = "aa a aa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character in string ignoring upper case", () => {
const str = "aaAaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character in string ignoring lower case", () => {
const str = "aAAaAAA";
const char = "A";
const count = countChar(str, char);
expect(count).toEqual(5);
});
17 changes: 16 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
export function getOrdinalNumber(num) {
return "1st";
let numString = num.toString();
if (11 <= Number(numString.slice(-2)) && 19 >= Number(numString.slice(-2))) {
return numString + "th";
} else if (
-11 >= Number(numString.slice(-2)) &&
-19 <= Number(numString.slice(-2))
) {
Comment on lines +3 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please be aware of the consistency of code formatting - while the first if using one line for the conditions but the second if using multiple lines for the conditions. I personally would prefer one line which may usually be easier to read, unless explicitly specified by coding standard in a business environment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I am aware of that, this is the doing of the auto format that we had to put into vscode. I tried to keep it like line 3 and it just automatically does that format, when I saw it doing that I tried to make it then be like lines 6 and 7 but then it forces it back to how it was at 3. Not sure how to stop it doing that when I hit tell it to save.

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 letting me know. I will discuss with cirriculum team to improve the auto format file. All good for now.

return numString + "th";
} else if (numString.slice(-1) === "1") {
return numString + "st";
} else if (numString.slice(-1) === "2") {
return numString + "nd";
} else if (numString.slice(-1) === "3") {
return numString + "rd";
}
return numString + "th";
}
30 changes: 30 additions & 0 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,33 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("should append 'th' for numbers ending with 11-19", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

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

test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

test("should append 'th' for all other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(25)).toEqual("25th");
expect(getOrdinalNumber(137)).toEqual("137th");
});

test("should be giving correct endings for negative numbers", () => {
expect(getOrdinalNumber(-2)).toEqual("-2nd");
expect(getOrdinalNumber(-23)).toEqual("-23rd");
expect(getOrdinalNumber(-137)).toEqual("-137th");
});
11 changes: 9 additions & 2 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export function repeatStr() {
export function repeatStr(str, count) {
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
// The goal is to re-implement that function, not to use it.
return "hellohellohello";
if (count < 0) {
throw new Error("negative counts are not valid");
}
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}
return result;
}
43 changes: 41 additions & 2 deletions practice-tdd/repeat-str.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your test cases are correct. Just for you to think a little bit more, besides const str = "hello", what special string should also be added to each of the test cases?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah i think I understand, ones where it has a space or symbols, even an empty one. Added those tests.

@hackertainment hackertainment Oct 22, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes, sometimes space or symbols can be boundary cases. In the context of this program, only empty and non-empty string matters. Empty string with any positive or zero count should still be empty. However, empty string with negative count should still throw error. Some trainees also return empty string for negative count which would not be quite accurate "for the context of this question". This is why we need to test empty string "" together with "hello"in each of your original test suites - not adding empty string as a separate test suite. Since your program is correct, I will let you pass this time (even testing of empty string for count = -3 is missing) but understand the design process should be your focus. You may also book a mentored coding session with a volunteer to learn more about test cases design. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ah didn't think of the negatives there, need to remeber those. I see what you mean, I'll look into the mentored coding session I got coming up to ask about it but will look into it aswell.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { repeatStr } from "./repeat-str.js";
// 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.

test("should repeat the string count times", () => {
test("should repeat the string count 3 times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
Expand All @@ -20,13 +19,53 @@ test("should repeat the string count times", () => {
// 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.
test("should repeat the string count 1 times", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should repeat the string count 0 times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

test("should repeat the empty string count 0 times", () => {
const str = "";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

test("should repeat the space in string count 3 times", () => {
const str = " ";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual(" ");
});

test("should repeat the symbols in string count 3 times", () => {
const str = "@%£$";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("@%£$@%£$@%£$");
});

// Case: Handle 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.
test("should throw an error 'negative counts are not valid'", () => {
const str = "hello";
const count = -3;
expect(() => repeatStr(str, count)).toThrow(
new Error("negative counts are not valid")
);
});
Loading