Skip to content
8 changes: 7 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count += 1;
}
}
return count;
}
60 changes: 60 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ import { countChar } from "./count.js";
// When the countChar function is called with these inputs,
// Then it should:

// Scenario: One Occurrence
// Given the input string `str`,
// And a character `char` that occurs one times in `str` (e.g., 'a' in 'abbbb'),
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.
test("should count one occurrence of a character at the beginning of a string", () => {
const str = "abbbb";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should count one occurrence of a character at the end of a string", () => {
const str = "bbbba";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should count one occurrence of a character in the middle of a string", () => {
const str = "bbabb";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(1);
});

// Scenario: Multiple Occurrences
// Given the input string `str`,
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
Expand All @@ -17,8 +43,42 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character when mixed with other characters", () => {
const str = "ababa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(3);
});

test("should count multiple occurrences of a character at the beginning and end of a string", () => {
const str = "abbba";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(2);
});

test("should count multiple occurrences of a character in the middle of a string", () => {
const str = "baaabaaabbb";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(7);
});

// 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.
test("should count 0 occurrences of a character", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should count 0 occurrences of a character for an empty string", () => {
const str = "";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});
18 changes: 17 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export function getOrdinalNumber(num) {
return "1st";
const numString = num.toString();
const lastDigit = numString.slice(-1);
const last2Digit = numString.slice(-2);

if (lastDigit === "1" && last2Digit !== "11") {
return numString + "st";
}

if (lastDigit === "2" && last2Digit !== "12") {
return numString + "nd";
}

if (lastDigit === "3" && last2Digit !== "13") {
return numString + "rd";
}

return numString + "th";
}
25 changes: 25 additions & 0 deletions practice-tdd/get-ordinal-number.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.

Good coverage of test cases.

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,28 @@ 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 '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 numbers ending not ending with 1,2,3, except those ending with 11, 12, 13", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(166)).toEqual("166th");
expect(getOrdinalNumber(238)).toEqual("238th");
expect(getOrdinalNumber(211)).toEqual("211th");
expect(getOrdinalNumber(312)).toEqual("312th");
expect(getOrdinalNumber(413)).toEqual("413th");
});
13 changes: 11 additions & 2 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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("Error - invalid count number");
}

let repeatedStr = "";
for (let i = count; i > 0; i--) {
repeatedStr += str;
}

return repeatedStr;
}
45 changes: 45 additions & 0 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.

I added tests for empty strings

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Well done.

Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,62 @@ test("should repeat the string count times", () => {
expect(repeatedStr).toEqual("hellohellohello");
});

test("should return empty string when str is empty and count = 3", () => {
const str = "";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

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

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

test("should return empty string when str is empty and count = 1", () => {
const str = "";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// 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 empty string when count = 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

test("should return empty string when string is empty and count = 0", () => {
const str = "";
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 return error when count < 0", () => {
const str = "hello";
const count = -5;
expect(() => repeatStr(str, count)).toThrow(/Error/);
});

test("should return error when count < 0 and string is empty", () => {
const str = "";
const count = -5;
expect(() => repeatStr(str, count)).toThrow(/Error/);
});
Loading