diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..bc055f3e5 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,11 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let numOfa = 0; + + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters.charAt(i) === findCharacter) { + numOfa++; + } + } + + return numOfa; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..2e4ac2d53 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,8 +17,36 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count multiple occurrences of a character, any string length", () => { + const str = "aaaaaa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(6); +}); + +test("any string length, varying chars in string", () => { + const str = "aaabaaa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(6); +}); + +test("any string length, varying chars in string, any char in string", () => { + const str = "aaabaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + // 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("any string length, varying chars in string, char NOT in string", () => { + const str = "aaabaaa"; + const char = "c"; + 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..edc44d340 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,31 @@ export function getOrdinalNumber(num) { - return "1st"; + if (num % 1 !== 0 || !num || typeof num !== "number" || num.length > 1) { + throw new Error("Error: whole number is needed"); + } + + if ((num - 11) % 100 === 0) { + return num + "th"; + } + + if ((num - 1) % 10 === 0) { + return num + "st"; + } + + if ((num - 12) % 100 === 0) { + return num + "th"; + } + + if ((num - 2) % 10 === 0) { + return num + "nd"; + } + + if ((num - 13) % 100 === 0) { + return num + "th"; + } + + if ((num - 3) % 10 === 0) { + 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..d547e1d16 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,76 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 11. Should append "th" to the number +test("should append 'th' for numbers ending with 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(4211)).toEqual("4211th"); +}); + +// Case 3: Numbers ending with 2 (but not 12). Should append "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(132)).toEqual("132nd"); +}); + +// Case 4: Numbers ending with 12. Should append "th" to the number +test("should append 'th' for numbers ending with 12", () => { + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(4212)).toEqual("4212th"); +}); + +// Case 5: Numbers ending with 3 (but not 13). Should append "rd" to the number +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); + +// Case 6: Numbers ending with 13. Should append "th" to the number +test("should append 'th' for numbers ending with 12", () => { + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + expect(getOrdinalNumber(4213)).toEqual("4213th"); +}); + +// Case 7: All other whole numbers. Should append "th" to the number +test("should append 'th' for all other whole numbers", () => { + expect(getOrdinalNumber(-1834)).toEqual("-1834th"); + expect(getOrdinalNumber(23151509)).toEqual("23151509th"); + expect(getOrdinalNumber(-100)).toEqual("-100th"); +}); + +// Case 8: Throw error for invalid numbers +test("should throw error for all invalid numbers", () => { + expect(() => { + getOrdinalNumber(5.7); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber(); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber("hey"); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber(undefined); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber(null); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber(false); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber(true); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber([1, 2, 3]); + }).toThrow("Error: whole number is needed"); + expect(() => { + getOrdinalNumber({ 4: "hey", bye: 39 }); + }).toThrow("Error: whole number is needed"); +}); diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..a6ed87a28 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,13 @@ -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("/Error: count should be non negative integer/"); + } + + let repeatedStr = ""; + + for (let i = 0; i < count; i++) { + repeatedStr += str; + } + + return repeatedStr; } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..12db4a2cd 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -9,24 +9,60 @@ import { repeatStr } from "./repeat-str.js"; // 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 times, count is 3", () => { const str = "hello"; const count = 3; const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); +test("should repeat the string count times, count is 4", () => { + const str = "hello"; + const count = 4; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hellohellohellohello"); +}); + +test("should repeat the string count times, count is 3, any string", () => { + const str = "Orb"; + const count = 3; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("OrbOrbOrb"); +}); + // 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 'hello' 1 time", () => { + 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 return empty string, count is 0", () => { + 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 error, count is negative", () => { + const str = "hello"; + const count = -1; + expect(() => { + repeatStr(str, count); + }).toThrow("/Error: count should be non negative integer/"); +});