diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..698900cc7 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 +function countChar(str, char) { + return str.split(char).length - 1; } module.exports = countChar; + +console.log( + countChar( + "The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.", + "a" + ) +); diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..bf48e6f9b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -2,7 +2,7 @@ const countChar = require("./count"); // Given a string str and a single character char to search for, // When the countChar function is called with these inputs, -// Then it should: +// Then it should: Return how many time the char occurs in the str. // Scenario: Multiple Occurrences // Given the input string str, @@ -22,3 +22,9 @@ 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 character does not exist in the string", () => { + const str = "hello world"; + const char = "z"; + 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..1e25cb4e8 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,43 @@ function getOrdinalNumber(num) { - return "1st"; + num = num.toString(); + if ( + num.slice(-2) === "11" || + num.slice(-2) === "12" || + num.slice(-2) === "13" + ) { + return num + "th"; + } + if (num.slice(-1) === "1") { + return num + "st"; + } + if (num.slice(-1) === "2") { + return num + "nd"; + } + if (num.slice(-1) === "3") { + return num + "rd"; + } + return num + "th"; } module.exports = getOrdinalNumber; + +console.log(getOrdinalNumber(1)); +console.log(getOrdinalNumber(2)); +console.log(getOrdinalNumber(3)); +console.log(getOrdinalNumber(4)); +console.log(getOrdinalNumber(5)); +console.log(getOrdinalNumber(6)); +console.log(getOrdinalNumber(7)); +console.log(getOrdinalNumber(8)); +console.log(getOrdinalNumber(9)); +console.log(getOrdinalNumber(10)); +console.log(getOrdinalNumber(11)); +console.log(getOrdinalNumber(12)); +console.log(getOrdinalNumber(13)); +console.log(getOrdinalNumber(21)); +console.log(getOrdinalNumber(22)); +console.log(getOrdinalNumber(23)); +console.log(getOrdinalNumber(101)); +console.log(getOrdinalNumber(111)); +console.log(getOrdinalNumber(112)); +console.log(getOrdinalNumber(113)); 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..a33cba2c0 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -4,10 +4,27 @@ const getOrdinalNumber = require("./get-ordinal-number"); // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback -// 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", () => { +test("append 'st' to numbers ending in 1,except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(101)).toEqual("101st"); +}); + +test("append 'nd' to numbers ending in 2, except those ending in 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +test("should return 'th' for numbers where the last digit is not 1, 2, or 3, excluding special cases 11–13", () => { + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(134)).toEqual("134th"); }); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..a8257d151 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,14 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } module.exports = repeat; + +console.log(repeat("hello", 3)); diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..71f040aa0 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -2,7 +2,7 @@ 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: +// Then it should: repeat the str count times and return a new string containing the repeated str values. // case: repeat String: // Given a target string str and a positive integer count, @@ -20,13 +20,32 @@ test("should repeat the string count times", () => { // 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. +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. +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 throw an error when count is negative", () => { + const str = "hello"; + const count = -1; + expect(() => repeat(str, count)).toThrow( + "Count must be a non-negative integer" + ); +});