Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
39 changes: 38 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (stringOfCharacters == null) {
return "Error: The input string cannot be null or undefined.";
}

if (stringOfCharacters.length === 0) {
return "Error: The string cannot be empty.";
}

const chars = [...stringOfCharacters];
const findChars = [...findCharacter];

if (!findCharacter || findChars.length !== 1) {
return "Error: The character to count must be a single character.";
}

let count = 0;
for (let ch of chars) {
if (ch === findCharacter) count++;
}

return count;
}

// Examples
console.log(countChar("hello", "l")); // Expected: 2
console.log(countChar("javascript", "a")); // Expected: 2
console.log(countChar("hello", "z")); // Expected: 0
console.log(countChar("null", "")); // Expected: "Error: The character to count must be a single character."
console.log(countChar("", "a")); // Expected: "Error: The string cannot be empty."
console.log(countChar("hello", "ll")); // Expected: "Error: The character to count must be a single character."
console.log(countChar("hipopotamos' make wonderful pets", "o")); // Expected: 3
console.log(countChar("hipopotamos", "p")); // Expected: 1
console.log(countChar("hipopotamos' are a friendly animal", "t")); // Expected: 1
console.log(countChar("hipopotamos", "x")); // Expected: 0
console.log(countChar("Pneumonoultramicroscopicsilicovolcanoconiosis", "i")); // Expected: 6
console.log(countChar("null", "")); // Expected: "Error: The character to count must be a single character."
console.log(countChar("", "a")); // Expected: "Error: The string cannot be empty."
console.log(countChar("hello", "ll")); // Expected: "Error: The character to count must be a single character."
console.log(countChar(null, "a")); // Expected: "Error: The input string cannot be null or undefined."

module.exports = countChar;
99 changes: 98 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const countChar = require("./count");
// 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";
Expand All @@ -22,3 +21,101 @@ test("should count multiple occurrences of a character", () => {
// 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.
test("should return 0 if the character does not exist in the string", () => {
expect(countChar("hello", "z")).toEqual(0);
});

// Scenario: Single Occurrence
// Given the input string str,
// And a character char that occurs exactly once in str,
// When the function is called with these inputs,
// Then it should return 1, indicating one occurrence of char in str.
test("should count a single occurrence of a character", () => {
expect(countChar("hello", "h")).toEqual(1);
});

// Scenario: Empty String
// Given an empty input string str,
// And any character char,
// When the function is called with these inputs,
// Then it should return 0, as there are no characters to count.
test("should return error if the string is empty", () => {
expect(countChar("", "a")).toEqual("Error: The string cannot be empty.");
});

// Scenario: Case Sensitivity
// Given the input string str containing both uppercase and lowercase versions of a letter,
// And a character char that matches only one case (either uppercase or lowercase),
// When the function is called with these inputs,
// Then it should count only the occurrences matching the exact case of char.
test("should count only characters that match the exact case", () => {
const str = "AaAaA";
const lowercaseResult = countChar(str, "a"); // lowercase only
const uppercaseResult = countChar(str, "A"); // uppercase only
expect(lowercaseResult).toEqual(2); // Only lowercase 'a's
expect(uppercaseResult).toEqual(3); // Only uppercase 'A's
});

// Scenario: Special Characters
// Given the input string str containing special or non-alphabetic characters,
// And a character char that matches one of those special characters,
// When the function is called with these inputs,
// Then it should return the number of times that special character occurs in str.
test("should count special characters", () => {
expect(countChar("!?!?", "?")).toEqual(2);
});

// Scenario: Spaces
// Given the input string str that contains space characters,
// And a character char that is a space,
// When the function is called with these inputs,
// Then it should return the number of space characters in str.
test("should count spaces correctly", () => {
expect(countChar("a b c", " ")).toEqual(2);
});

// Scenario: Null or Undefined Input (Optional Defensive Case)
// Given a null or undefined input string str,
// And a character char,
// When the function is called with these inputs,
// Then it should safely return 0 instead of throwing an error.
test("should return error if the string is null or undefined", () => {
expect(countChar(null, "a")).toEqual(
"Error: The input string cannot be null or undefined."
);
expect(countChar(undefined, "a")).toEqual(
"Error: The input string cannot be null or undefined."
);
});

// Scenario: Character Not a Single Character (Optional Defensive Case)
// Given the input string str,
// And a character char that is either an empty string or has more than one character,
// When the function is called with these inputs,
// Then it should return an error indicating that char must be a single character.
test("should return error if the character to count is not a single character", () => {
expect(countChar("hello", "")).toEqual(
"Error: The character to count must be a single character."
);
expect(countChar("hello", "ll")).toEqual(
"Error: The character to count must be a single character."
);
});

// Scenario: Unicode Characters
// Given the input string str containing Unicode characters (e.g., emojis),
// And a character char that is a Unicode character,
// When the function is called with these inputs,
// Then it should return the number of times that Unicode character occurs in str.
test("should count unicode characters correctly", () => {
expect(countChar("😀😃😀😄", "😀")).toEqual(2);
});
// Scenario: Long Strings
// Given a very long input string str,
// And a character char that occurs multiple times in str,
// When the function is called with these inputs,
// Then it should efficiently return the correct count of char in str.
test("should handle long strings efficiently", () => {
const longString = "a".repeat(10000) + "b".repeat(5000) + "a".repeat(3000);
expect(countChar(longString, "a")).toEqual(13000);
});
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