diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..2f3f292f02 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ 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 42baf4b4b0..3ccf951999 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,24 @@ 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("returns 0 if the character is not in the string", () => { + const str = "bread"; + const char = "z"; + const count = countChar(str, char); + expect(count).toBe(0); +}); + +test("returns 1 when the character appears once in the string", () => { + const str = "bread"; + const char = "d"; + const count = countChar(str, char); + expect(count).toBe(1); +}); + +test("counts how many times a character appears", () => { + const str = "breadboard"; + const char = "b"; + const count = countChar(str, char); + expect(count).toBe(2); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..4951da7dc7 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,19 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + if (lastTwoDigits >= 11 && lastTwoDigits <= 13){ + return num+ "th"; + } + const lastDigit = num %10; + switch (lastDigit){ + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "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 dfe4b60916..f24f8067e0 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,6 +8,37 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { +test("should append 'st' when the number ends with 1 but not 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(101)).toEqual("101st"); +}); + +test("should append 'nd' when the number ends with 2 (but not 12)", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(102)).toEqual("102nd"); +}); + +test("should append 'rd' when the number ends with 3 (but not 13)", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(103)).toEqual("103rd"); +}); + +test("should append 'th' when the number ends 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"); +}); + +test("should append 'th' when the number ends with 0, 4, 5, 6, 7, 8, or 9", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(24)).toEqual("24th"); + expect(getOrdinalNumber(100)).toEqual("100th"); }); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3c..90c796af08 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,8 @@ -function repeat() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("count cannot be negative"); + } + return str.repeat(count); } -module.exports = repeat; +module.exports = repeatStr; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09cf..e6bbfca376 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -1,5 +1,5 @@ // Implement a function repeat -const repeat = require("./repeat"); +const repeatStr = require("./repeat"); // Given a target string str and a positive integer count, // When the repeat function is called with these inputs, // Then it should: @@ -12,7 +12,7 @@ const repeat = require("./repeat"); test("should repeat the string count times", () => { const str = "hello"; const count = 3; - const repeatedStr = repeat(str, count); + const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); @@ -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("returns the original string when count is 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toBe("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("returns an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toBe(""); +}); + // 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("throws an error when count is negative", () => { + const str = "hello"; + const count = -3; + expect(() => repeatStr(str, count)).toThrow("count cannot be negative"); +}); \ No newline at end of file