diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..ffdb077ed 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,10 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + return count; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..a493d8553 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -10,15 +10,35 @@ 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", () => { +test("should count a character when every character in the string matches", () => { const str = "aaaaa"; const char = "a"; const count = countChar(str, char); expect(count).toEqual(5); }); +test("should count multiple occurrences of a character in a mixed string", () => { + const str = "occurrence"; + const char = "c"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); + // Scenario: No Occurrences // Given the input string `str`, // 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 return 0 when character does not occur", () => { + const str = "aaaabcd"; + const char = "e"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should return 0 when string is empty", () => { + const str = ""; + const char = "e"; + 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..1d361428a 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,17 @@ export function getOrdinalNumber(num) { - return "1st"; + if (num % 100 === 11) { + return `${num}th`; + } else if (num % 10 === 1) { + return `${num}st`; + } else if (num % 100 === 12) { + return `${num}th`; + } else if (num % 10 === 2) { + return `${num}nd`; + } else if (num % 100 === 13) { + return `${num}th`; + } else if (num % 10 === 3) { + return `${num}rd`; + } else { + return `${num}th`; + } } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..288bfc805 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,31 @@ 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" to numbers ending in 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 "nd" to numbers ending in 2, apart from those ending in 12', () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); + expect(getOrdinalNumber(142)).toEqual("142nd"); +}); + +test('should append "rd" to numbers ending in 3, apart from those ending in 13', () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(143)).toEqual("143rd"); +}); + +test(`should append "th" to other numbers that don't end in 1, 2, or 3`, () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(24)).toEqual("24th"); + expect(getOrdinalNumber(100)).toEqual("100th"); +}); diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..36828f586 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,10 @@ -export function repeatStr() { - // 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"; +export function repeatStr(str, count) { + if (count < 0) { + throw new Error("warning! Count cannot be negative!"); + } + 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..85ca34f67 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -16,17 +16,60 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); +test("should return an empty string when repeating an empty string multiple times", () => { + const str = ""; + const count = 3; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + // 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 return the original string when count equals 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); + +test("should return an empty string when count equals 1 and the string is empty", () => { + const str = ""; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); // 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 when count equals 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); +test("should return an empty string when count equals 0 and the string is empty", () => { + const str = ""; + 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 when count is a negative number", () => { + const str = "hello"; + const count = -2; + expect(() => repeatStr(str, count)).toThrow(); +}); + +test("should throw an error when count is negative and the string is empty", () => { + const str = ""; + const count = -2; + expect(() => repeatStr(str, count)).toThrow(); +});