diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..4f95d5f20 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,7 @@ -export function countChar(stringOfCharacters, findCharacter) { - return 5 +export function countChar(str, char) { + let charMatch = str.match(new RegExp(char, "g")); + if (charMatch === null) { + return 0; + } + return charMatch.length; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..db380714e 100644 --- a/practice-tdd/count.test.js +++ b/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 `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. + +test("should count 0 with no occurrences of a character", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..69b5f9f96 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,18 @@ export function getOrdinalNumber(num) { - return "1st"; + let numString = num.toString(); + if (11 <= Number(numString.slice(-2)) && 19 >= Number(numString.slice(-2))) { + return numString + "th"; + } else if ( + -11 >= Number(numString.slice(-2)) && + -19 <= Number(numString.slice(-2)) + ) { + return numString + "th"; + } else if (numString.slice(-1) === "1") { + return numString + "st"; + } else if (numString.slice(-1) === "2") { + return numString + "nd"; + } else if (numString.slice(-1) === "3") { + return numString + "rd"; + } + return numString + "th"; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..33f87e04d 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,33 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +test("should append 'th' for numbers ending with 11-19", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(113)).toEqual("113th"); +}); + +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +test("should append 'th' for all other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(25)).toEqual("25th"); + expect(getOrdinalNumber(137)).toEqual("137th"); +}); + +test("should be giving correct endings for negative numbers", () => { + expect(getOrdinalNumber(-2)).toEqual("-2nd"); + expect(getOrdinalNumber(-23)).toEqual("-23rd"); + expect(getOrdinalNumber(-137)).toEqual("-137th"); +}); diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..36c13f5d9 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,12 @@ -export function repeatStr() { +export function repeatStr(str, count) { // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; + if (count < 0) { + throw new Error("negative counts are not valid"); + } + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..5093fb792 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -8,8 +8,7 @@ import { repeatStr } from "./repeat-str.js"; // Given a target string `str` and a positive integer `count` greater than 1, // When the repeatStr function is called with these inputs, // Then it should return a string that contains the original `str` repeated `count` times. - -test("should repeat the string count times", () => { +test("should repeat the string count 3 times", () => { const str = "hello"; const count = 3; const repeatedStr = repeatStr(str, count); @@ -20,13 +19,32 @@ 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("should repeat the string count 1 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("should repeat the string count 0 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. +test("should throw an error 'negative counts are not valid'", () => { + const str = "hello"; + const count = -3; + expect(() => repeatStr(str, count)).toThrow( + new Error("negative counts are not valid") + ); +});