diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..fc0e0925a 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,22 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let arrayOfCharacters = []; + if (typeof stringOfCharacters === "string") { + arrayOfCharacters = stringOfCharacters.split(""); + } else if (Array.isArray(stringOfCharacters)) { + arrayOfCharacters = stringOfCharacters; + } else if (typeof stringOfCharacters === "number") { + arrayOfCharacters = stringOfCharacters.toString().split(""); + } else if (typeof stringOfCharacters === "object") { + return 0; + } + + 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..67cf4b5d4 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,34 @@ 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); +}); + +// test for str is an array +test("should return 0 when str is an array", () => { + const str = ["a", "b", "c"]; + const char = "a"; + const count = countChar(str, char); + expect(parseInt(count)).toEqual(1); +}); + +// test for str is a number +test("should return 0 when str is a number", () => { + const str = 12345; + const char = "3"; + const count = countChar(str, char); + expect(parseInt(count)).toEqual(1); +}); 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..7309730c9 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,12 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if ((Number(count) < 0) | (typeof str === "object")) { + throw new Error("Invalid data value"); + } + try { + return String(str).repeat(Number(count)); + } catch (error) { + throw new Error("Invalid data value"); + } } 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..962193fb0 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -20,13 +20,84 @@ 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. +test("should return an empty string when count is 0", () => { + const str = "hello"; + 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 when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Invalid data value"); +}); + +// case: str is not a string: +// Given a non-string input for str (e.g., a number) and a positive integer count, +// When the repeat function is called with these inputs, +// Then it should convert the non-string str to a string and repeat it count times, returning the appropriately repeated string. +test("should convert non-string str to string and repeat it count times", () => { + const str = 123; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("123123"); +}); + +// case: count is not a number: +// Given a target string str and a non-numeric input for count (e.g., a string that can be converted to a number), +// When the repeat function is called with these inputs, +// Then it should convert the non-numeric count to a number and repeat the str that many times, returning the appropriately repeated string. +test("should convert non-numeric count to number and repeat the string that many times", () => { + const str = "hello"; + const count = "3"; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hellohellohello"); +}); + +// 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 round down the count to the nearest integer and repeat the str that many times, returning the appropriately repeated string. +test("should round down decimal count to nearest integer and repeat the string that many times", () => { + const str = "hello"; + const count = 3.7; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hellohellohello"); +}); + +// 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: invalid str and count types: +// Given invalid types for both str (e.g., an object) and count (e.g., 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 input types are not supported. +test("should throw an error when str and count are of invalid types", () => { + const str = { text: "hello" }; + const count = [3]; + expect(() => repeat(str, count)).toThrow("Invalid data value"); +});