diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..2604d29f3 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (const i of stringOfCharacters) { + if (i === findCharacter) { + count++; + } + } + + return count; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..18135fafe 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-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 not find any occurrences", () => { + const str = "aaaaaaaa"; + const char = "b"; + + 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..198b2f03d 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,31 @@ function getOrdinalNumber(num) { - return "1st"; + // handle special cases + + if (typeof num !== "number" || Number.isNaN(num)) { + throw new Error(`Invalid type for ${num}, expected Number`); + } + + const snum = String(num); // num as a string + const thList = ["0", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]; // list of numbers that should end up with th as a suffix + + const lastCharIndex = snum.length - 1; + const lastOne = snum[lastCharIndex]; + const lastTwo = snum.slice(lastCharIndex - 1); + + if (thList.includes(lastTwo) || thList.includes(lastOne)) { + return snum + "th"; + } + + switch (lastOne) { + case "1": + return snum + "st"; + case "2": + return snum + "nd"; + case "3": + return snum + "rd"; + default: + break; + } } module.exports = getOrdinalNumber; 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 adfa58560..66848d8a4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,66 @@ 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 '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 numbers ending with 0, 4-9, 10, 11, 12, 13", () => { + // 0 + expect(getOrdinalNumber(0)).toEqual("0th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(100)).toEqual("100th"); + // 4 + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(24)).toEqual("24th"); + expect(getOrdinalNumber(134)).toEqual("134th"); + + // 5 + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(25)).toEqual("25th"); + expect(getOrdinalNumber(135)).toEqual("135th"); + + // 6 + expect(getOrdinalNumber(6)).toEqual("6th"); + expect(getOrdinalNumber(26)).toEqual("26th"); + expect(getOrdinalNumber(136)).toEqual("136th"); + + // 7 + expect(getOrdinalNumber(7)).toEqual("7th"); + expect(getOrdinalNumber(27)).toEqual("27th"); + expect(getOrdinalNumber(137)).toEqual("137th"); + + // 8 + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(28)).toEqual("28th"); + expect(getOrdinalNumber(138)).toEqual("138th"); + + // 9 + expect(getOrdinalNumber(9)).toEqual("9th"); + expect(getOrdinalNumber(29)).toEqual("29th"); + expect(getOrdinalNumber(139)).toEqual("139th"); + + // 10 + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(100)).toEqual("100th"); + + // 11, 12, 13 + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// edge cases +test("should throw an error when given a wrong input type", () => { + expect(() => getOrdinalNumber("testing string")).toThrow("Invalid type for "); + expect(() => getOrdinalNumber("")).toThrow(); + expect(() => getOrdinalNumber(null)).toThrow(); + expect(() => getOrdinalNumber(undefined)).toThrow(); +}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea..176f35f62 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,17 @@ -function repeatStr() { +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("Expected a positive integer"); + } + + let out = ""; + + for (let i = 0; i < count; i++) { + out = out + str; + } + + return out; } module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..b2f055e73 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,31 @@ test("should repeat the string count times", () => { // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("Should return the original string with no repetitions", () => { + 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 an empty string", () => { + 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, as negative counts are not valid", () => { + const str = "hello"; + const count = -1; + expect(() => repeatStr(str, count)).toThrow("Expected a positive integer"); +});