diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..7b6986706 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,13 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + let position = stringOfCharacters.indexOf(findCharacter); + if (position === -1) { + return 0; + } else { + while (position !== -1) { + count++; + position = stringOfCharacters.indexOf(findCharacter, position + 1); + } + return count; + } } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..9e8088045 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -10,6 +10,13 @@ import { countChar } from "./count.js"; // When the function is called with these inputs, // Then it should correctly count occurrences of `char`. +test("should count multiple occurrences of a character", () => { + const str = "hello"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + test("should count multiple occurrences of a character", () => { const str = "aaaaa"; const char = "a"; @@ -17,6 +24,34 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "Shirin"; + const char = "h"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should count multiple occurrences of a character", () => { + const str = "This ia a second string test"; + const char = "s"; + const count = countChar(str, char); + expect(count).toEqual(4); +}); + +test("should count multiple occurrences of a character", () => { + const str = ""; + const char = "t"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should count multiple occurrences of a character", () => { + const str = "TTTT"; + const char = "t"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + // Scenario: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`. diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..bf0bee67f 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,15 @@ export function getOrdinalNumber(num) { - return "1st"; + if (num % 10 === 1 && num % 100 != 11) { + return `${num}st`; + } + + if (num % 10 === 2 && num % 100 != 12) { + return `${num}nd`; + } + + if (num % 10 === 3 && num % 100 != 13) { + return `${num}rd`; + } + + return `${num}th`; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..c54ac202c 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -11,10 +11,42 @@ import { getOrdinalNumber } from "./get-ordinal-number.js"; // This approach improves coverage and makes our tests easier to maintain. // Case 1: Numbers ending with 1 (but not 11) + // When the number ends with 1, except those ending with 11, // Then the function should return a string by appending "st" to the number. test("should append 'st' for numbers ending with 1, except those ending with 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); + expect(getOrdinalNumber(241)).toEqual("241st"); +}); + +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +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(32)).toEqual("32nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +// Case 3: Numbers ending with 3 (but not 13) +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(123)).toEqual("123rd"); +}); + +// Case 3: Numbers ending in other digits +test("should append 'th' for numbers ending in other digits", () => { + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + expect(getOrdinalNumber(13)).toBe("13th"); + expect(getOrdinalNumber(5)).toBe("5th"); + expect(getOrdinalNumber(10)).toBe("10th"); + expect(getOrdinalNumber(20)).toBe("20th"); + expect(getOrdinalNumber(111)).toBe("111th"); + expect(getOrdinalNumber(113)).toBe("113th"); + expect(getOrdinalNumber(112)).toBe("112th"); }); diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..b9b241cfe 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 strRep = ""; + for (let i = 0; i < count; i++) { + strRep += str; + } + return strRep; } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..305c3dc5f 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -1,5 +1,6 @@ // Implement a function repeatStr import { repeatStr } from "./repeat-str.js"; +import { test, expect } from "@jest/globals"; // Given a target string `str` and a positive integer `count`, // When the repeatStr function is called with these inputs, // Then it should: @@ -15,18 +16,39 @@ test("should repeat the string count times", () => { const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); - // Case: handle count of 1: // 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 times", () => { + const str = "World"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("World"); +}); // 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 return an empty string for zero", () => { + const str = "Hi!"; + 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, as negative counts are not valid", () => { + const str = "Hi!"; + const count = -4; + expect(() => repeatStr(str, -4)).toThrow("negative counts are not valid"); +}); + +test("should repeat the string count times", () => { + const str = ""; + const count = 5; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +});