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
12 changes: 11 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
let position = stringOfCharacters.indexOf(findCharacter);
if (position === -1) {
return 0;
} else {
while (position !== -1) {
count++;
position = stringOfCharacters.indexOf(findCharacter, position + 1);
}
return count;
}
}
35 changes: 35 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,48 @@ import { countChar } from "./count.js";
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

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

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

test("should count multiple occurrences of a character", () => {
const str = "Shirin";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should count multiple occurrences of a character", () => {
const str = "This ia a second string test";
const char = "s";
const count = countChar(str, char);
expect(count).toEqual(4);
});

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

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

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
Expand Down
14 changes: 13 additions & 1 deletion practice-tdd/get-ordinal-number.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.

This program seems not quite right to me. Why input cannot be ended with 11 or 22 or 33 ? They should all be valid. Please check the definition of ordinal number carefully, and fix the program (with test cases). 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.

Thanks for the feedback! I've corrected the ordinal number tests to handle the special cases ending in 11, 12, and 13, and added test cases for numbers ending in 22 and 33 to ensure they return nd and rd respectively.

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, and it looks better now.

Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
export function getOrdinalNumber(num) {
return "1st";
if (num % 10 === 1 && num % 100 != 11) {
return `${num}st`;
}

if (num % 10 === 2 && num % 100 != 12) {
return `${num}nd`;
}

if (num % 10 === 3 && num % 100 != 13) {
return `${num}rd`;
}

return `${num}th`;
}
32 changes: 32 additions & 0 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,42 @@ import { getOrdinalNumber } from "./get-ordinal-number.js";
// This approach improves coverage and makes our tests easier to maintain.

// Case 1: Numbers ending with 1 (but not 11)

// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(241)).toEqual("241st");
});

// 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(32)).toEqual("32nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

// Case 3: Numbers ending with 3 (but not 13)
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(123)).toEqual("123rd");
});

// Case 3: Numbers ending in other digits
test("should append 'th' for numbers ending in other digits", () => {
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(12)).toBe("12th");
expect(getOrdinalNumber(13)).toBe("13th");
expect(getOrdinalNumber(5)).toBe("5th");
expect(getOrdinalNumber(10)).toBe("10th");
expect(getOrdinalNumber(20)).toBe("20th");
expect(getOrdinalNumber(111)).toBe("111th");
expect(getOrdinalNumber(113)).toBe("113th");
expect(getOrdinalNumber(112)).toBe("112th");
});
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 strRep = "";
for (let i = 0; i < count; i++) {
strRep += str;
}
return strRep;
}
28 changes: 25 additions & 3 deletions practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Implement a function repeatStr
import { repeatStr } from "./repeat-str.js";
import { test, expect } from "@jest/globals";
// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:
Expand All @@ -15,18 +16,39 @@ test("should repeat the string count times", () => {
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// 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", () => {
const str = "World";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("World");
});
// 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 for zero", () => {
const str = "Hi!";
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, as negative counts are not valid", () => {
const str = "Hi!";
const count = -4;
expect(() => repeatStr(str, -4)).toThrow("negative counts are not valid");
});

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