diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..d94e9e82d 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,10 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) + if (stringOfCharacters[i] === findCharacter) { + count++; + } + 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..00223ac19 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,36 @@ 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 for no occurrences of the character", () => { + const str = "hello world"; + const char = "z"; + const count = countChar(str, char); // Calls countChar with the test inputs and saves the returned value + expect(count).toEqual(0); // checks that your function returns 0 for that test case. +}); + +// Scenario: Single Occurrence +// Given the input string str, +// And a character char that appears exactly once in the case-sensitive str, +// When the function is called with these inputs, +// Then it should return 1, indicating a single occurrence of the char was found in the str. + +test("Should count the ocurrences of the character", () => { + const str = "hello world"; + const char = "h"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +// Scenario: Case Sensitivity +// Given the input string str containing both uppercase and lowercase versions of a character, +// And a character char that matches only one case, +// When the function is called with these inputs, +// Then it should count only the exact case matches, not both. + +test("Should count the uppercase ocurrences of the character", () => { + const str = "heLLo world"; + const char = "L"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..ba1a8bdc4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,23 @@ -function getOrdinalNumber(num) { - return "1st"; +function getOrdinalNumber(inputNumber) { + inputNumber = Number(inputNumber); // Convert input to a number (handles string inputs edge case) + const absoluteNumber = Math.abs(inputNumber); // Use absolute value (handles negatives) + const lastTwo = absoluteNumber % 100; // Get the last two digits (for 'teens' exception) + let suffix = "th"; // Default suffix is 'th' + + if (lastTwo < 11 || lastTwo > 13) { + // || is the "or" operator i learned by solving this function + // If not 11, 12, or 13, use last digit rule + const lastDigit = absoluteNumber % 10; // Get the last digit + + if (lastDigit === 1) { + suffix = "st"; // 1st, 21st, etc. + } else if (lastDigit === 2) { + suffix = "nd"; // 2nd, 22nd, etc. + } else if (lastDigit === 3) { + suffix = "rd"; // 3rd, 23rd, etc. + } + } + return inputNumber + suffix; // Return the number with the correct suffix } -module.exports = getOrdinalNumber; +module.exports = getOrdinalNumber; // Export the function for use in other files 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..a1d6fd1d5 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,59 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +// While i was doing my research I stumbled upon .toBe for exact equality. +// .toBe() checks for exact equality using === (strict equality). +// You can use it for primitive values: strings, numbers, booleans, null, or undefined. + +// Basics +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toBe("2nd"); +}); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toBe("3rd"); +}); + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toBe("4th"); +}); + +// Teens ordinal numbers +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toBe("11th"); +}); + +test("should return '12th' for 12", () => { + expect(getOrdinalNumber(12)).toBe("12th"); +}); + +test("should return '13th' for 13", () => { + expect(getOrdinalNumber(13)).toBe("13th"); +}); + +// Last-digit rule +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toBe("21st"); +}); + +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toBe("22nd"); +}); + +test("should return '23rd' for 23", () => { + expect(getOrdinalNumber(23)).toBe("23rd"); +}); + +test("should return '24th' for 24", () => { + expect(getOrdinalNumber(24)).toBe("24th"); +}); + +// Edge cases +test("should return '0th' for 0", () => { + expect(getOrdinalNumber(0)).toBe("0th"); +}); + +test("should return '-1st' for -1", () => { + expect(getOrdinalNumber(-1)).toBe("-1st"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..e6a194277 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,19 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (count < 0) { + throw new Error("Count must be non-negative number"); // 'throw' creates and sends an error, stopping the function immediately + } + if (count === 0) { + return ""; + } + if (count === 1) { + return str; + } + let result = ""; // Start with an empty string to build the result + for (let i = 0; i < count; i++) { + // Loop 'count' times (i starts at 0, goes up to count +1) + result += str; // Used an addition assignment operator. instead of result = result + str + } + return result; } 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..1f239e31d 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -21,12 +21,32 @@ test("should repeat the string count times", () => { // 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 count a single occurrence of the character", () => { + 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 trow an error when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count must be non-negative number"); +});