diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..58e897046 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let num = 0; + if (findCharacter) { + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + num++; + } + } + return `${findCharacter} appears ${num} time(s).`; + } else return 0; } +// console.log(countChar("Hello", "l")); + 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..da864257b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,10 @@ 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 a character that does not exist", () => { + const str = "aaaaa"; + const char = "b"; + 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..7ca87fb73 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,11 @@ function getOrdinalNumber(num) { + if (num > 1 || num < 1) { + return "No 1st"; + } + return "1st"; } +console.log(getOrdinalNumber(1)); + 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..c4736d6b3 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,16 @@ -const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber +function getOrdinalNumber(num) { + const remainder10 = num % 10; + const remainder100 = num % 100; -// continue testing and implementing getOrdinalNumber for additional cases -// Write your tests using Jest - remember to run your tests often for continual feedback + if (remainder10 === 1 && remainder100 !== 11) { + return `${num}st`; + } else if (remainder10 === 2 && remainder100 !== 12) { + return `${num}nd`; + } else if (remainder10 === 3 && remainder100 !== 13) { + return `${num}rd`; + } else { + return `${num}th`; + } +} -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" - -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); -}); +module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..be4a3df0e 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,5 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, times) { + return str.repeat(times); } 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..10999a2c6 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -1,14 +1,6 @@ -// Implement a function repeat const repeat = require("./repeat"); -// Given a target string str and a positive integer count, -// When the repeat function is called with these inputs, -// Then it should: - -// case: repeat String: -// Given a target string str and a positive integer count, -// When the repeat function is called with these inputs, -// Then it should repeat the str count times and return a new string containing the repeated str values. +// case: repeat String test("should repeat the string count times", () => { const str = "hello"; const count = 3; @@ -16,17 +8,25 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); -// case: handle Count of 1: -// 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. +// case: handle Count of 1 +test("should return the original string 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. +// case: Handle Count of 0 +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. +// case: Negative Count +test("should throw an error when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +});