diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..9137773b1 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,25 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let arrayOfCharacters = []; + if (typeof stringOfCharacters === "string") { + arrayOfCharacters = stringOfCharacters.split(""); + } else { + throw new Error("Input str should be a string"); + } + + if (typeof findCharacter !== "string") { + throw new Error("Input char should be a string"); + } + if (findCharacter.length !== 1) { + throw new Error("Input char should be a single character"); + } + + let count = 0; + for (let index = 0; index < arrayOfCharacters.length; index++) { + if (arrayOfCharacters[index] === findCharacter) { + count += 1; + } + } + return count; } 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..ef82682f0 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -14,7 +14,7 @@ test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; const count = countChar(str, char); - expect(count).toEqual(5); + expect(parseInt(count)).toEqual(5); }); // Scenario: No Occurrences @@ -22,3 +22,141 @@ 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 when character does not exist in the string", () => { + const str = "abcdefg"; + const char = "h"; + const count = countChar(str, char); + expect(parseInt(count)).toEqual(0); +}); + +// test for empty string +test("should return 0 when string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(parseInt(count)).toEqual(0); +}); + +// tests for unhappy paths (invalid str) +const errorMessageForString = "Input str should be a string"; + +// test for str is an array +test("should throw an error when str is an array", () => { + const str = ["a", "b", "c"]; + const char = "a"; + expect(() => countChar(str, char)).toThrow(errorMessageForString); +}); + +// test for str is a number +test("should throw an error when str is a number", () => { + const str = 12345; + const char = "1"; + expect(() => countChar(str, char)).toThrow(errorMessageForString); +}); + +// test for str is an object +test("should throw an error when str is an object", () => { + const str = { a: 1, b: 2 }; + const char = "a"; + expect(() => countChar(str, char)).toThrow(errorMessageForString); +}); + +// test for str is null +test("should throw an error when str is null", () => { + const str = null; + const char = "a"; + expect(() => countChar(str, char)).toThrow(errorMessageForString); +}); + +// test for str is undefined +test("should throw an error when str is undefined", () => { + const str = undefined; + const char = "a"; + expect(() => countChar(str, char)).toThrow(errorMessageForString); +}); + +// test for str is a boolean +test("should throw an error when str is a boolean", () => { + const str = true; + const char = "a"; + expect(() => countChar(str, char)).toThrow(errorMessageForString); +}); + +// tests for unhappy paths (invalid char) +const errorMessageForCharacter = "Input char should be a string"; + +// test for char is an array +test("should throw an error when char is an array", () => { + const str = "abcdefg"; + const char = ["a", "b"]; + expect(() => countChar(str, char)).toThrow(errorMessageForCharacter); +}); + +// test for char is a number +test("should throw an error when char is a number", () => { + const str = "abcdefg"; + const char = 1; + expect(() => countChar(str, char)).toThrow(errorMessageForCharacter); +}); + +// test for char is an object +test("should throw an error when char is an object", () => { + const str = "abcdefg"; + const char = { a: 1 }; + expect(() => countChar(str, char)).toThrow(errorMessageForCharacter); +}); + +// test for char is null +test("should throw an error when char is null", () => { + const str = "abcdefg"; + const char = null; + expect(() => countChar(str, char)).toThrow(errorMessageForCharacter); +}); + +// test for char is undefined +test("should throw an error when char is undefined", () => { + const str = "abcdefg"; + const char = undefined; + expect(() => countChar(str, char)).toThrow(errorMessageForCharacter); +}); + +// test for char is a boolean +test("should throw an error when char is a boolean", () => { + const str = "abcdefg"; + const char = true; + expect(() => countChar(str, char)).toThrow(errorMessageForCharacter); +}); + +// test for char is more than one character +const errorMessageForSingleCharacter = + "Input char should be a single character"; + +test("should throw an error when char is more than one character", () => { + const str = "abcdefg"; + const char = "ab"; + expect(() => countChar(str, char)).toThrow(errorMessageForSingleCharacter); +}); + +// test for char is a space character +test("should return 0 when char is a space character", () => { + const str = "abcdefg"; + const char = " "; + const count = countChar(str, char); + expect(parseInt(count)).toEqual(0); +}); + +// test for str is a space character +test("should return 0 when str is a space character", () => { + const str = " "; + const char = "a"; + const count = countChar(str, char); + expect(parseInt(count)).toEqual(0); +}); + +//test for char is an empty string +test("should throw an error when char is an empty string", () => { + const str = "abcdefg"; + const char = ""; + expect(() => countChar(str, char)).toThrow(errorMessageForSingleCharacter); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..2e5d1f166 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; + const stringNum = String(num); + const paddedLast2Digit = stringNum.padStart(2, "0").slice(-2); + const numberValue = Number(paddedLast2Digit); + + if (numberValue >= 11 && numberValue <= 13) { + return `${stringNum}th`; + } + if (stringNum.endsWith("1")) { + return `${stringNum}st`; + } + if (stringNum.endsWith("2")) { + return `${stringNum}nd`; + } + if (stringNum.endsWith("3")) { + return `${stringNum}rd`; + } + return `${stringNum}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..58d6f3430 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,63 @@ 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 '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 '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); +}); +test("should return '24th' for 24", () => { + expect(getOrdinalNumber(24)).toEqual("24th"); +}); +test("should return '111th' for 111", () => { + expect(getOrdinalNumber(111)).toEqual("111th"); +}); +test("should return '112th' for 112", () => { + expect(getOrdinalNumber(112)).toEqual("112th"); +}); +test("should return '113th' for 113", () => { + expect(getOrdinalNumber(113)).toEqual("113th"); +}); +test("should return '121st' for 121", () => { + expect(getOrdinalNumber(121)).toEqual("121st"); +}); +test("should return '1000011th' for 1000011", () => { + expect(getOrdinalNumber(1000011)).toEqual("1000011th"); +}); +test("should return '1000002nd' for 1000002", () => { + expect(getOrdinalNumber(1000002)).toEqual("1000002nd"); +}); +test("should return '0th' for 0", () => { + expect(getOrdinalNumber(0)).toEqual("0th"); +}); +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"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..e3fe4b832 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,11 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (typeof str !== "string") { + throw new Error("Input str should be a string in format 'Hello'"); + } + if (!Number.isInteger(count) || count <= 0) { + throw new Error("Count should be a positive integer number"); + } + return str.repeat(count); } 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..2d1f3bc74 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,167 @@ 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 string 1 time when count is 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hello"); +}); // 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. +// Then it should throw an error or return an appropriate error message, as repeating a string zero times is not valid. +test("should throw an error when count is 0", () => { + const str = "hello"; + const count = 0; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); // 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 when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is a decimal number: +// Given a target string str and a decimal number for count, +// When the repeat function is called with these inputs, +// Then it should throw an error or return an appropriate error message, indicating that the count should be a positive integer number. +test("should throw an error when count is a decimal number", () => { + const str = "hello"; + const count = 2.5; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is not an integer number: +// Given a target string str and a non-integer input for count (e.g., a string or an array), +// When the repeat function is called with these inputs, +// Then it should throw an error or return an appropriate error message, indicating that the count should be a positive integer number. +test("should throw an error when count is not a number", () => { + const str = "hello"; + const count = "abc"; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is an array: +test("should throw an error when count is an array", () => { + const str = "hello"; + const count = [2]; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is an object: +test("should throw an error when count is an object", () => { + const str = "hello"; + const count = { num: 2 }; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is null: +test("should throw an error when count is null", () => { + const str = "hello"; + const count = null; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is a boolean: +test("should throw an error when count is a boolean", () => { + const str = "hello"; + const count = true; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: count is undefined: +test("should throw an error when count is undefined", () => { + const str = "hello"; + const count = undefined; + expect(() => repeat(str, count)).toThrow( + "Count should be a positive integer number" + ); +}); + +// case: str is an empty string: +// Given an empty string for str and a positive integer count, +// When the repeat function is called with these inputs, +// Then it should return an empty string, as repeating an empty string any number of times still results in an empty string. +test("should return an empty string when str is an empty string", () => { + const str = ""; + const count = 5; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); + +// case: str is not a string eg: number: +test("should throw an error when str is a number", () => { + const str = 123; + const count = 2; + expect(() => repeat(str, count)).toThrow( + "Input str should be a string in format 'Hello'" + ); +}); + +// case: str is an array: +test("should throw an error when str is an array", () => { + const str = ["hello"]; + const count = 2; + expect(() => repeat(str, count)).toThrow( + "Input str should be a string in format 'Hello'" + ); +}); + +// case: str is an object: +test("should throw an error when str is an object", () => { + const str = { text: "hello" }; + const count = 2; + expect(() => repeat(str, count)).toThrow( + "Input str should be a string in format 'Hello'" + ); +}); + +// case: str is null: +test("should throw an error when str is null", () => { + const str = null; + const count = 2; + expect(() => repeat(str, count)).toThrow( + "Input str should be a string in format 'Hello'" + ); +}); + +// case: str is a boolean: +test("should throw an error when str is a boolean", () => { + const str = true; + const count = 2; + expect(() => repeat(str, count)).toThrow( + "Input str should be a string in format 'Hello'" + ); +}); + +// case: str is undefined: +test("should throw an error when str is undefined", () => { + const str = undefined; + const count = 2; + expect(() => repeat(str, count)).toThrow( + "Input str should be a string in format 'Hello'" + ); +});