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
33 changes: 32 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
// 1. Validate 'stringOfCharacters' input
if (stringOfCharacters === null || stringOfCharacters === undefined) {
throw new Error("The input string cannot be null or undefined.");
}
if (typeof stringOfCharacters !== "string") {
throw new Error("The input string must be a string type.");
}
Comment on lines +3 to +8
Copy link
Contributor

Choose a reason for hiding this comment

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

Why treat null and undefined differently from other non-string types?


// 2. Validate 'findCharacter' input
if (typeof findCharacter !== "string" || findCharacter.length !== 1) {
// This handles null, undefined, and strings that aren't a single character (including empty string "")
throw new Error(
"The character to count must be a single character string."
);
}

// 3. Handle the empty string case (returns 0)
if (stringOfCharacters.length === 0) {
return 0;
}

// 4. Perform the count
let count = 0;
for (let ch of stringOfCharacters) {
if (ch === findCharacter) {
count++;
}
}

return count;
}

module.exports = countChar;

// implement a function countChar that counts the number of times a character occurs in a string
111 changes: 94 additions & 17 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,101 @@
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");
// const countChar = require("./count");
// Given a string str and a single character char to search for,
// When the countChar function is called with these inputs,
// Then it should:

// Scenario: Multiple Occurrences
// Given the input string str,
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').

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

describe("countChar - Standard character counting", () => {
test.each([
["aaaaa", "a", 5],
["hello", "l", 2],
["javascript", "a", 2],
["hello", "z", 0],
["hello", "h", 1],
["hipopotamos' make wonderful pets", "o", 4],
["hipopotamos", "p", 2],
["hipopotamos' are friendly animals", "t", 1],
["hipopotamos", "x", 0],
["Pneumonoultramicroscopicsilicovolcanoconiosis", "i", 6],
])("should count occurrences of '%s' in '%s'", (input, char, expected) => {
expect(countChar(input, char)).toBe(expected);
});
});

describe("countChar - Edge cases", () => {
test("should return 0 for empty string", () => {
expect(countChar("", "a")).toBe(0);
});

test("should return 0 when character is not found", () => {
expect(countChar("abcdef", "z")).toBe(0);
});

test("should handle special characters", () => {
expect(countChar("!@#$%^&*", "&")).toBe(1);
});

test("should be case-sensitive", () => {
expect(countChar("AaAaA", "a")).toBe(2);
expect(countChar("AaAaA", "A")).toBe(3);
});

test("should handle numeric characters", () => {
expect(countChar("123123123", "2")).toBe(3);
});

test("should handle whitespace characters", () => {
expect(countChar("a b c d e", " ")).toBe(4);
});
});

// Scenario: No Occurrences
// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
describe("countChar - Input validation", () => {
test("should throw error if input string is null", () => {
expect(() => countChar(null, "a")).toThrow(
"The input string cannot be null or undefined."
);
});

test("should throw error if input string is undefined", () => {
expect(() => countChar(undefined, "a")).toThrow(
"The input string cannot be null or undefined."
);
});

test("should throw error if input string is not a string", () => {
expect(() => countChar(12345, "a")).toThrow(
"The input string must be a string type."
);
});

test("should throw error if findCharacter is null", () => {
expect(() => countChar("abc", null)).toThrow(
"The character to count must be a single character string."
);
});

test("should throw error if findCharacter is undefined", () => {
expect(() => countChar("abc", undefined)).toThrow(
"The character to count must be a single character string."
);
});

test("should throw error if findCharacter is not a string", () => {
expect(() => countChar("abc", 1)).toThrow(
"The character to count must be a single character string."
);
});

test("should throw error if findCharacter is an empty string", () => {
expect(() => countChar("abc", "")).toThrow(
"The character to count must be a single character string."
);
});

test("should throw error if findCharacter is more than one character", () => {
expect(() => countChar("abc", "ab")).toThrow(
"The character to count must be a single character string."
);
});
});
17 changes: 14 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function getOrdinalNumber(num) {
return "1st";
function getOrdinalNumber(input) {
const lastDigit = input % 10;
const lastTwoDigits = input % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return input + "th";
} else if (lastDigit === 1) {
return input + "st";
} else if (lastDigit === 2) {
return input + "nd";
} else if (lastDigit === 3) {
return input + "rd";
} else {
return input + "th";
}
}

module.exports = getOrdinalNumber;
64 changes: 63 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,69 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"
test('should return "2nd" for input 2', () => {
expect(getOrdinalNumber(2)).toBe("2nd");
});

// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"
test('should return "3rd" for input 3', () => {
expect(getOrdinalNumber(3)).toBe("3rd");
});

// Case 4: Handle special case for 11
// When the number is 11,
// Then the function should return "11th"
test('should return "11th" for input 11', () => {
expect(getOrdinalNumber(11)).toBe("11th");
});

// Case 5: Handle special case for 12
// When the number is 12,
// Then the function should return "12th"
test('should return "12th" for input 12', () => {
expect(getOrdinalNumber(12)).toBe("12th");
});

// Case 6: Handle special case for 13
// When the number is 13,
// Then the function should return "13th"
test('should return "13th" for input 13', () => {
expect(getOrdinalNumber(13)).toBe("13th");
});

// Case 7: Identify the ordinal number for 21
// When the number is 21,
// Then the function should return "21st"
test('should return "21st" for input 21', () => {
expect(getOrdinalNumber(21)).toBe("21st");
});

// Case 8: Identify the ordinal number for 102
// When the number is 102,
// Then the function should return "102nd"
test('should return "102nd" for input 102', () => {
expect(getOrdinalNumber(102)).toBe("102nd");
});

// Case 9: Identify the ordinal number for 113
// When the number is 113,
// Then the function should return "113th"
test('should return "113th" for input 113', () => {
expect(getOrdinalNumber(113)).toBe("113th");
});

// Case 10: Identify the ordinal number for 100
// When the number is 100,
// Then the function should return "100th"
test('should return "100th" for input 100', () => {
expect(getOrdinalNumber(100)).toBe("100th");
});
7 changes: 5 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (typeof str !== "string") throw new Error("Input must be a string");
if (typeof count !== "number" || count < 0)
throw new Error("Count must be a non-negative number");
return str.repeat(count);
}

module.exports = repeat;
28 changes: 28 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,41 @@ test("should repeat the string count times", () => {
// Given a target string str and a count equal to 1,
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
test("should return the original string when count is 1", () => {
const str = "world";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("world");
});

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
test("should return an empty string when count is 0", () => {
const str = "test";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});

// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
test("should throw an error for a negative count", () => {
const str = "error";
const count = -2;
expect(() => repeat(str, count)).toThrow(
"Count must be a non-negative number"
);
});
// case: Non-String Input:
// Given a non-string input for str (e.g., a number, object, or array) and a positive integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as the function is expected to handle only string inputs.
test("should throw an error for non-string input", () => {
const str = 12345; // Non-string input
const count = 2;
expect(() => repeat(str, count)).toThrow("Input must be a string");
});
Loading