diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..26a4575e68 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,5 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + return stringOfCharacters.split(findCharacter).length - 1; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..356e782c5e 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -16,9 +16,33 @@ test("should count multiple occurrences of a character", () => { const count = countChar(str, char); expect(count).toEqual(5); }); +test("should count multiple occurrences of a character", () => { + const str = "international"; + const char = "n"; + const count = countChar(str, char); + expect(count).toEqual(3); +}); +test("should count multiple occurrences of a character", () => { + const str = "crisis"; + const char = "s"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); // 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, indicating that no occurrences of `char` were found", () => { + const str = "interesting"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); +test("should return 0, indicating that no occurrences of `char` were found", () => { + const str = "future"; + const char = "m"; + 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 f95d71db13..3d148bd771 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,18 @@ function getOrdinalNumber(num) { - return "1st"; + if ([11, 12, 13].includes(num % 100)) { + return num + "th"; + } + if (String(num).at(-1) === "1") { + return num + "st"; + } + if (String(num).at(-1) === "2") { + return num + "nd"; + } + if (String(num).at(-1) === "3") { + return num + "rd"; + } else { + return num + "th"; + } } 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 adfa58560f..d8d2534934 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -17,4 +17,30 @@ 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(1021)).toEqual("1021st"); +}); +//case 2; Numbers ending with 4,5,6,7,8,9 and 0 +test("should append 'th' for numbers ending with 4,5,6,7,8,9,0", () => { + expect(getOrdinalNumber(104)).toEqual("104th"); + expect(getOrdinalNumber(2000)).toEqual("2000th"); + expect(getOrdinalNumber(167)).toEqual("167th"); + expect(getOrdinalNumber(19)).toEqual("19th"); +}); +//case 3; Numbers ending with 11,12,13 +test("should append 'th' for numbers ending with 11,12,13", () => { + expect(getOrdinalNumber(1013313)).toEqual("1013313th"); + expect(getOrdinalNumber(2012)).toEqual("2012th"); + expect(getOrdinalNumber(16711)).toEqual("16711th"); +}); +//case 4;Numbers ending with 2 +test("should append 'nd' for numbers ending with 2", () => { + expect(getOrdinalNumber(102)).toEqual("102nd"); + expect(getOrdinalNumber(2022)).toEqual("2022nd"); + expect(getOrdinalNumber(16732)).toEqual("16732nd"); +}); +//case 4;Numbers ending with 3 +test("should append 'nd' for numbers ending with 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(2023)).toEqual("2023rd"); + expect(getOrdinalNumber(16733)).toEqual("16733rd"); }); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..3aea5bc87a 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,14 @@ -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"; + let result = ""; + if (count < 0) { + throw new Error("Invalid count"); + } else { + for (let i = 0; i < count; i++) { + result += str; + } + return result; + } } - 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 a3fc1196c4..ae2de6a192 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -15,18 +15,56 @@ test("should repeat the string count times", () => { const repeatedStr = repeatStr(str, count); expect(repeatedStr).toEqual("hellohellohello"); }); +test("should repeat the string count times", () => { + const str = "codeyourfuture"; + const count = 2; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("codeyourfuturecodeyourfuture"); +}); // 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 once", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); +test("should repeat the string once", () => { + const str = "codeyourfuture"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("codeyourfuture"); +}); // 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(""); +}); +test("should return an empty string", () => { + const str = "codeyourfuture"; + 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 for negative integer", () => { + const str = "hello"; + const count = -4; + expect(() => repeatStr(str, count)).toThrow("Invalid count"); +}); +test("should throw an error for negative integer", () => { + const str = "codeyourfuture"; + const count = -1; + expect(() => repeatStr(str, count)).toThrow("Invalid count"); +});