diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..8c2e24f00 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,18 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + // trying to find the amount of times the findCharacter is found in the stringOfCharacters + const lengthOfString = stringOfCharacters.length; + console.log("lengthOfString", lengthOfString); + + // replace all instances of findCharacter in string of characters with an empty string, and then get the length. (e.g. in house replace the e with an empty string " "). + const otherLettersLength = stringOfCharacters.replaceAll( + findCharacter, + "" + ).length; + + // declaring the variable called result and subtract otherLettersLength from the lengthOfString to find the number of occurences of findCharacter. + const result = lengthOfString - otherLettersLength; + console.log("result", result); + return result; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..f9e440fb7 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -10,15 +10,37 @@ const countChar = require("./count"); // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. -test("should count multiple occurrences of a character", () => { +test("should count all occurrences of a character when it appears multiple times", () => { const str = "aaaaa"; const char = "a"; const count = countChar(str, char); expect(count).toEqual(5); }); +test("should return 1 when the character appears only once in the string", () => { + const str = "house"; + const char = "e"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should correctly count occurrences of a character appearing twice", () => { + const str = "playful"; + const char = "l"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + // 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. + +// test(``); +test("should return when there are no occurrences of a character", () => { + const str = "spaceship"; + const char = "x"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..b65ddfc59 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,33 @@ function getOrdinalNumber(num) { - return "1st"; +<<<<<<< Updated upstream + const lastdigit = num % 10; + if (lastdigit === 1) { + return `${num}st`; + } + + if (lastdigit === 2) { + return `${num}nd`; + } + + if (lastdigit === 3) { + return `${num}rd`; + } else { + return `${num}th`; + } +======= + const lastDigit = num % 10; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + if (lastDigit === 1) return num + "st"; + if (lastDigit === 2) return num + "nd"; + if (lastDigit === 3) return num + "rd"; + + return num + "th"; +>>>>>>> Stashed changes } 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 adfa58560..37918e326 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,54 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +<<<<<<< Updated upstream +test("should append 'nd' for numbers ending with 2", () => { +======= +// 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("Numbers ending with 2 (but not 12) should return 'nd'", () => { +>>>>>>> Stashed changes + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +<<<<<<< Updated upstream +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); +======= +// 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("Numbers ending with 3 (but not 13) should return 'rd'", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Case 4: Numbers ending with 0,4-9 +// When the number ends with 0, 4, 5, 6, 7, 8, or 9, +// Then the function should return a string by appending "th" to the number +test("Numbers ending with 0,4-9 should return 'th'", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(25)).toEqual("25th"); +}); + +// Case 5: Numbers ending with 11, 12, or 13 +// When the number ends with 11, 12, or 13, +// Then the function should return a string by appending "th" to the number +test("Numbers ending with 11,12,13 should return 'th'", () => { + 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"); +}); +>>>>>>> Stashed changes diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..163d9a741 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,29 @@ -function repeatStr() { - return "hellohellohello"; -} +function repeatStr(str, count) { + if (count < 0) { +<<<<<<< Updated upstream + throw new Error("invalid count"); + } + return str.repeat(count); +======= + throw new Error("Count cannot be negative"); + } + + if (count === 0) { + return ""; + } + + if (count === 1) { + return str; + } + let result = ""; + + for (let i = 0; i < count; i++) { + result += str; + } + + return result; +>>>>>>> Stashed changes +} +// call the repeat function on the string and pass in count. module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..8c7d81ba6 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,45 @@ 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("given a string and count of 1, returns the original string", () => { + expect(repeatStr("hello", 1)).toBe("hello"); +}); + +test("should repeat the string count times", () => { + 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("given a string and count of 0, returns an empty string", () => { + expect(repeatStr("hello", 0)).toBe(""); +}); + +test("should repeat the string count times", () => { + 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. +<<<<<<< Updated upstream + +test("should repeat the string count times", () => { + const str = "hello"; + const count = -2; + // const repeatedStr = repeatStr(str, count); + expect(() => repeatStr(str, count)).toThrow("invalid count"); +======= +test("given a string and a negative count, throws an error", () => { + expect(() => repeatStr("hello", -1)).toThrow(); +>>>>>>> Stashed changes +});