diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..6bac6cb49 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,5 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + // Given a string `stringOfCharacters` and a single character `findCharacter` to search for, + const count = stringOfCharacters.split(findCharacter).length - 1; + return count } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..45d86e833 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -10,6 +10,7 @@ import { countChar } from "./count.js"; // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. +// Case1: Multiple Occurrences in a string test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; @@ -17,8 +18,63 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +// Case2: Multiple Occurrences in a string with different characters +test("should count multiple occurrences of a character in a string with different characters", () => { + const str = "abacadaeaf"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(5); +}); + +// Case3: Multiple Occurrences in different cases, case sensitive +test("should count multiple occurrences of a character in different cases", () => { + const str = "AaAaAa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + +// Scenario: Single Occurrence +// Given the input string `str`, +// And a character `char` that occurs exactly once in `str` (e.g., 'a' in 'abc'), +// When the function is called with these inputs, +// Then it should return 1, indicating a single occurrence of `char`. + +// Case4: Single Occurrence in a string with different characters +test("should count a single occurrence of a character", () => { + const str = "abc"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + // Scenario: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. + +//Case5: No Occurrences in a string with different characters +test("should return 0 when character does not occur in the string", () => { + const str = "abc"; + const char = "d"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Edge Cases +// Case6: Empty String +test("should return 0 when the input string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Case7: String contains spaces +test("should count occurrences of a character in a string with spaces", () => { + const str = "a b c a b c"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); \ No newline at end of file diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..396267397 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,16 @@ export function getOrdinalNumber(num) { - return "1st"; + //Given a number `num`, the function should return a string representing the ordinal form of that number. + //When the number ends with 1, except those ending with 11, + //Then the function should return a string by appending "st" to the number. + if (num % 10 === 1 && num % 100 !== 11) return `${num}st`; + + //When the number ends with 2, except those ending with 12, + //Then the function should return a string by appending "nd" to the number. + if (num % 10 === 2 && num % 100 !== 12) return `${num}nd`; + + //When the number ends with 3, except those ending with 13, + //Then the function should return a string by appending "rd" to the number. + if (num % 10 === 3 && num % 100 !== 13) return `${num}rd`; + + return `${num}th`; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..f33b9d013 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,49 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +// Case 3: Numbers ending with 3 (but not 13) +// When the number ends with 3, except those ending with 13, +// Then the function should return a string by appending "rd" to the number. +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Case 4: Numbers ending with 11, 12, or 13 +test("should append 'th' for numbers ending with 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); +}); + +// Case 5: All other numbers +test("should append 'th' for all other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(6)).toEqual("6th"); + expect(getOrdinalNumber(7)).toEqual("7th"); + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(9)).toEqual("9th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(14)).toEqual("14th"); + expect(getOrdinalNumber(15)).toEqual("15th"); + expect(getOrdinalNumber(16)).toEqual("16th"); + expect(getOrdinalNumber(17)).toEqual("17th"); + expect(getOrdinalNumber(18)).toEqual("18th"); + expect(getOrdinalNumber(19)).toEqual("19th"); + expect(getOrdinalNumber(20)).toEqual("20th"); +}); \ No newline at end of file diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..e71816056 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,20 @@ -export function repeatStr() { +export function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count must be a positive integer"); + } + if (count === 0) { + return ""; + } + // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; + let result = ""; + for (let i = 0; i < count; i++) { // i++ is i=i+1, i+=1, i=i+1, all are valid + result = result + str; //result += str; is also valid + } + return result; + + } + + diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..7d822c5c0 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -20,13 +20,37 @@ test("should repeat the string count times", () => { // Given a target string `str` and a `count` equal to 1, // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("should return the original string when count is 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); // Case: Handle count of 0: // Given a target string `str` and a `count` equal to 0, // When the repeatStr function is called with these inputs, // Then it should return an empty string. +test("should return an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // Case: Handle negative count: // Given a target string `str` and a negative integer `count`, // When the repeatStr function is called with these inputs, // Then it should throw an error, as negative counts are not valid. +test("should throw an error when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeatStr(str, count)).toThrow(Error); +}); + +test ("should repeat the string count times", () => { + const str = "abc"; + const count = 4; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("abcabcabcabc"); +});