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
4 changes: 3 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
// Given a string `stringOfCharacters` and a single character `findCharacter` to search for,
const count = stringOfCharacters.split(findCharacter).length - 1;
return count
Comment on lines +3 to +4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Statement not consistently terminated by a semicolon. Why not use a formatter?

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.

Screenshot 2026-10-21 095436 Thanks for pointing this out. This seems weird to me because I have turned on Prettier; I chose formatting on save in setting. Just now I also double-checked bar on bottom right Prettier has been in place. I will take a look of this. In the meantime, is there situation where Prettier (or formatter) doesn't apply (e.g. for specific language only), and require further configuration to keep it on?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Prettier only support certain languages. For example, I know it does not support Python.

Here are two possible conditions I know from experience that Prettier may not work on JS code:

  • The code has a syntax error (not your case)
  • You have more than one VS Code extensions that can format JS code. If this is the case, when you manually select "Format document" on JS code, VS Code may prompt you to select a default formatter for JS code and you could then select Prettier as your formatter. After that, "Format on save" should work.

}
56 changes: 56 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,71 @@ import { countChar } from "./count.js";
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

// Case1: Multiple Occurrences in a string
test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

// Case2: Multiple Occurrences in a string with different characters
test("should count multiple occurrences of a character in a string with different characters", () => {
const str = "abacadaeaf";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

// Case3: Multiple Occurrences in different cases, case sensitive
test("should count multiple occurrences of a character in different cases", () => {
const str = "AaAaAa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(3);
});

// Scenario: Single Occurrence
// Given the input string `str`,
// And a character `char` that occurs exactly once in `str` (e.g., 'a' in 'abc'),
// When the function is called with these inputs,
// Then it should return 1, indicating a single occurrence of `char`.

// Case4: Single Occurrence in a string with different characters
test("should count a single occurrence of a character", () => {
const str = "abc";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(1);
});

// Scenario: No Occurrences
// Given the input string `str`,
// 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.

//Case5: No Occurrences in a string with different characters
test("should return 0 when character does not occur in the string", () => {
const str = "abc";
const char = "d";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Edge Cases
// Case6: Empty String
test("should return 0 when the input string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Case7: String contains spaces
test("should count occurrences of a character in a string with spaces", () => {
const str = "a b c a b c";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(2);
});
15 changes: 14 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export function getOrdinalNumber(num) {
return "1st";
//Given a number `num`, the function should return a string representing the ordinal form of that number.
//When the number ends with 1, except those ending with 11,
//Then the function should return a string by appending "st" to the number.
if (num % 10 === 1 && num % 100 !== 11) return `${num}st`;

//When the number ends with 2, except those ending with 12,
//Then the function should return a string by appending "nd" to the number.
if (num % 10 === 2 && num % 100 !== 12) return `${num}nd`;

//When the number ends with 3, except those ending with 13,
//Then the function should return a string by appending "rd" to the number.
if (num % 10 === 3 && num % 100 !== 13) return `${num}rd`;

return `${num}th`;
}
46 changes: 46 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,49 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2 (but not 12)
// When the number ends with 2, except those ending with 12,
// Then the function should return a string by appending "nd" to the number.
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");
});

// Case 3: Numbers ending with 3 (but not 13)
// When the number ends with 3, except those ending with 13,
// Then the function should return a string by appending "rd" to the number.
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");
});

// Case 4: Numbers ending with 11, 12, or 13
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

// Case 5: All other numbers
test("should append 'th' for all other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(6)).toEqual("6th");
expect(getOrdinalNumber(7)).toEqual("7th");
expect(getOrdinalNumber(8)).toEqual("8th");
expect(getOrdinalNumber(9)).toEqual("9th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(14)).toEqual("14th");
expect(getOrdinalNumber(15)).toEqual("15th");
expect(getOrdinalNumber(16)).toEqual("16th");
expect(getOrdinalNumber(17)).toEqual("17th");
expect(getOrdinalNumber(18)).toEqual("18th");
expect(getOrdinalNumber(19)).toEqual("19th");
expect(getOrdinalNumber(20)).toEqual("20th");
});
19 changes: 17 additions & 2 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
export function repeatStr() {
export function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be a positive integer");
}
if (count === 0) {
return "";
}

// 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";
let result = "";
for (let i = 0; i < count; i++) { // i++ is i=i+1, i+=1, i=i+1, all are valid
result = result + str; //result += str; is also valid
}
Comment on lines +12 to +14

@cjyuan cjyuan Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any of them will do.

Suggestion: Use AI to explore "how to use comments in code?", and then clean up the comments.

return result;


}


24 changes: 24 additions & 0 deletions practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,37 @@ 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 return the original string when count is 1", () => {
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 return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
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 when count is negative", () => {
const str = "hello";
const count = -2;
expect(() => repeatStr(str, count)).toThrow(Error);
});

test ("should repeat the string 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.

Can you think of a better name for this test so it differs from others?

@AlanGit-debug2604 AlanGit-debug2604 Oct 20, 2026 •

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.

What about: test ("string should repeat itself for number of counts");

const str = "abc";
const count = 4;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("abcabcabcabc");
});
Loading