diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..c3b27cd1b 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,23 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + // Check both inputs are strings + if ( + typeof stringOfCharacters !== "string" || + typeof findCharacter !== "string" + ) { + return "Invalid input: input should be a string"; + } + + // Convert both to lowercase for case-insensitive matching + const str = stringOfCharacters.toLowerCase(); + const char = findCharacter.toLowerCase(); + + // Check that only one character is passed + if (char.length !== 1) { + return "invalid input: Input just one character"; + } + + // Return count (0 for empty strings works naturally) + return str.split(char).length - 1; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..69e5fae75 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,71 @@ 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 occurence of a character", () => { + const str = "happy"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: One occurrence +// Given an input string `str`, +// And a character `char` that occurs once within `str` (e.g., 'a' in 'boat'), +// When the function is called with these inputs, +// Then it should correctly return the count of `char` (e.g., 'a' appears once in 'boat'). + +test("should return 1 occurence of a character", () => { + const str = "boat"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should return 0 occurrence of character in an empty string", () => { + const str = ""; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should throw an error when more than one string is passed as char", () => { + const str = "jump"; + const char = "pp"; + const count = countChar(str, char); + expect(count).toEqual("invalid input: Input just one character"); +}); + +test("should count characters regardless of case (treat 'a' and 'A' as equal)", () => { + const str = "JUMP"; + const char = "m"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should return correct count of space characters", () => { + const str = "F r o NT"; + const char = " "; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + +test("should count numeric characters correctly in the string", () => { + const str = "2Languages6"; + const char = "2"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should correctly count characters in a very long string", () => { + const str = "b".repeat(500) + "abB"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(502); +}); + +test("should count numeric characters correctly in the string", () => { + const str = "2Languages6"; + const char = 2; + const count = countChar(str, char); + expect(count).toEqual("Invalid input: input should be a string"); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..f39df5b98 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,32 @@ function getOrdinalNumber(num) { - return "1st"; + // Early return for invalid inputs + if (!Number.isInteger(num)) { + return "Invalid input: Input is an integer"; + } + + const absNum = Math.abs(num); + const lastTwo = absNum % 100; + const lastDigit = absNum % 10; + + // Handle special cases: 11th, 12th, 13th + if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) { + return `${num}th`; + } + + // Handle regular suffixes + if (lastDigit === 1) { + return `${num}st`; + } + + if (lastDigit === 2) { + return `${num}nd`; + } + + if (lastDigit === 3) { + return `${num}rd`; + } + // Default suffix + return `${num}th`; } module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..efb242ef5 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,81 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +test("should return '10th' for 10", () => { + expect(getOrdinalNumber(10)).toEqual("10th"); +}); + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +test("should return '12th' for 12", () => { + expect(getOrdinalNumber(12)).toEqual("12th"); +}); + +test("should return '13th' for 13", () => { + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); + +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); + +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +test("should return '100th' for 100", () => { + expect(getOrdinalNumber(100)).toEqual("100th"); +}); + +test("should return '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); + +test("should return '0th' for 0", () => { + expect(getOrdinalNumber(0)).toEqual("0th"); +}); + +test("should return '-111th' for -111", () => { + expect(getOrdinalNumber(-111)).toEqual("-111th"); +}); + +test("should return 'Invalid input: Input is an integer' for 1.52", () => { + expect(getOrdinalNumber(1.52)).toEqual("Invalid input: Input is an integer"); +}); + +test("should return 'Invalid input: Input is an integer' for null", () => { + expect(getOrdinalNumber(null)).toEqual("Invalid input: Input is an integer"); +}); + +test("should return 'Invalid input: Input is an integer' for undefined", () => { + expect(getOrdinalNumber(undefined)).toEqual( + "Invalid input: Input is an integer" + ); +}); + +test("should return 'Invalid input: Input is an integer' for 'a'", () => { + expect(getOrdinalNumber("a")).toEqual("Invalid input: Input is an integer"); +}); + +test("should return 'Invalid input: Input is an integer' for {}", () => { + expect(getOrdinalNumber({})).toEqual("Invalid input: Input is an integer"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..af477773c 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,28 @@ -function repeat() { - return "hellohellohello"; +function repeat(valueToRepeat, numOfTimes) { + // Validate valueToRepeat + if (typeof valueToRepeat !== "string") { + return "Invalid input: valueToRepeat should be a string"; + } + // Validate numOfTimes + if (!Number.isInteger(numOfTimes)) { + return "Invalid input: numOfTimes should be an integer"; + } + + if (numOfTimes < 0) { + return "Negative number invalid"; + } + if (numOfTimes === 0) { + return ""; + } + + + // Repeat the string + let repeatedValue = ""; + for (let i = 0; i < numOfTimes; i++) { + repeatedValue += valueToRepeat; + } + + return repeatedValue; } module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..5428d4f39 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,128 @@ 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 input with no repetition", () => { + const str = "milk"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(str); +}); // 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 for zero count times", () => { + const str = "rice"; + 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 return an error message when negative number count is passed", () => { + const str = "food"; + const count = -2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Negative number invalid"); +}); + +test("should return an empty string when empty string is expected count number of times", () => { + const str = ""; + const count = 23; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); + +test("should return an error message when a number is passed as input", () => { + const str = 1; + const count = 3; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message when 'true' is passed as input", () => { + const str = true; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message for 'null' as input", () => { + const str = null; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message for 'undefined' as input", () => { + const str = undefined; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +// case: array input +test("should return an error message for 'array' as input", () => { + const str = []; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +// case: Non-integer positive count +test("should return an error message for non-integer positive count", () => { + const str = "apple"; + const count = 2.5; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Invalid input: numOfTimes should be an integer"); +}); + +// case: Non-integer negative count +test("should return an error message for non-integer negative count", () => { + const str = "banana"; + const count = -1.7; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("Invalid input: numOfTimes should be an integer"); +}); + +// case: Object input +test("should return an error message when object is passed as input", () => { + const str = {}; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message when input is not a string and count is not a number", () => { + const str = {}; + const count = -2.2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +}); + +test("should return an error message when input is not a string and count is not a number", () => { + const str = {}; + const count = -2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual( + "Invalid input: valueToRepeat should be a string" + ); +});