diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index ca1dfe7f2..96060c389 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -60,4 +60,4 @@ const obtuse = getAngleType(120); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file +// ====> write your test here, and then add a line to pass the test in the function above diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..72ff20f1d 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,17 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if ( + typeof stringOfCharacters !== "string" || + typeof findCharacter !== "string" || + findCharacter.length !== 1 + ) { + return 0; + } + + if (stringOfCharacters.length === 0) { + return 0; + } + + 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 42baf4b4b..cc6c0199c 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -2,7 +2,8 @@ const countChar = require("./count"); // Given a string str and a single character char to search for, // When the countChar function is called with these inputs, -// Then it should: +// Then it should: correctly count the occurrences of char in str. + // Scenario: Multiple Occurrences // Given the input string str, @@ -17,8 +18,89 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); + // Scenario: No Occurrences // Given the input string str, // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +test("should return 0 for no occurrences", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + + +// Scenario: Multiple occurrences +test("should count multiple occurrences of a character", () => { + const str = "aaaaa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(5); +}); + +// Scenario: No occurrences +test("should return 0 for no occurrences", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Empty string +test("should return 0 when input string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Empty character +test("should return 0 when character input is empty", () => { + const str = "hello"; + const char = ""; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Character longer than 1 +test("should return 0 when character input is longer than one character", () => { + const str = "hello"; + const char = "ll"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: char string longer than str string +test("should return 0 when char is longer than the input string", () => { + const str = "a"; + const char = "abc"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Non-string inputs +test("should return 0 when inputs are not strings", () => { + expect(countChar(12345, "1")).toEqual(0); + expect(countChar("12345", 1)).toEqual(0); + expect(countChar(null, "a")).toEqual(0); + expect(countChar("hello", undefined)).toEqual(0); +}); + +// Scenario: Case sensitivity +test("should count only exact case matches", () => { + const str = "AaAaA"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + +// Scenario: Spaces and special characters +test("should correctly count spaces and symbols", () => { + const str = "a b c d e ! !"; + const char = " "; + const count = countChar(str, char); + expect(count).toEqual(6); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..d5f60ecc5 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,34 @@ function getOrdinalNumber(num) { - return "1st"; + if (typeof num !== "number" || isNaN(num)) { + return "Invalid input"; + } + const sign = num < 0 ? "-" : ""; + num = Math.abs(num); + + // Handle decimals + if (!Number.isInteger(num)) { + return sign + num + "th"; + } + + const lastTwo = num % 100; + const lastOne = num % 10; + + // Handle special cases 11th, 12th, 13th + if (lastTwo >= 11 && lastTwo <= 13) { + return sign + num + "th"; + } + + // Normal ordinal rules + switch (lastOne) { + case 1: + return sign + num + "st"; + case 2: + return sign + num + "nd"; + case 3: + return sign + num + "rd"; + default: + return sign + num + "th"; + } } -module.exports = getOrdinalNumber; +module.exports = getOrdinalNumber; \ No newline at end of file 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 dfe4b6091..b8e535f4a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,87 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +/// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// Then the function should return "2nd" +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// Then the function should return "3rd" +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +// Case 4: Identify the ordinal number for 4 +// When the number is 4, +// Then the function should return "4th" +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +// Case 5: Identify the ordinal number for 11 +// When the number is 11, +// Then the function should return "11th" +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +// Case 6: Identify the ordinal number for 22 +// When the number is 22, +// Then the function should return "22nd" +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); + +// Case 7: Identify the ordinal number for 33 +// When the number is 33, +// Then the function should return "33rd" +test("should return '33rd' for 33", () => { + expect(getOrdinalNumber(33)).toEqual("33rd"); +}); + +// Case 8: Undefined input +// When the input is undefined, +// Then the function should return "Invalid input" +test("should handle undefined input gracefully", () => { + expect(getOrdinalNumber(undefined)).toEqual("Invalid input"); +}); + +// Case 9: Null input +// When the input is null, +// Then the function should return "Invalid input" +test("should handle null input gracefully", () => { + expect(getOrdinalNumber(null)).toEqual("Invalid input"); +}); + +// Case 10: Boolean input +// When the input is a boolean value, +// Then the function should return "Invalid input" +test("should handle boolean input gracefully", () => { + expect(getOrdinalNumber(true)).toEqual("Invalid input"); +}); + +// Case 11: Non-numeric string input +// When the input is a non-numeric string, +// Then the function should return "Invalid input" +test("should handle non-numeric string input gracefully", () => { + expect(getOrdinalNumber("hello")).toEqual("Invalid input"); +}); + +// Case 12: Negative number +// When the input is a negative number, +// Then the function should return the ordinal form with a negative sign +test("should return '-1st' for -1", () => { + expect(getOrdinalNumber(-1)).toEqual("-1st"); +}); + +// Case 13: Floating number +// When the input is a floating number, +// Then the function should return the ordinal with decimal intact +test("should handle decimal numbers", () => { + expect(getOrdinalNumber(21.5)).toEqual("21.5th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..ff4e6dca6 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,20 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (str === undefined || count === undefined){ + throw new Error ("argument must be defined") + } + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } module.exports = repeat; + + + diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..e16a3c248 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -2,7 +2,7 @@ const repeat = require("./repeat"); // Given a target string str and a positive integer count, // When the repeat function is called with these inputs, -// Then it should: +// Then it should: repeat the str count times and return a new string containing the repeated str values. // case: repeat String: // Given a target string str and a positive integer count, @@ -11,22 +11,80 @@ const repeat = require("./repeat"); test("should repeat the string count times", () => { const str = "hello"; - const count = 3; + const count = 4; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("hellohellohello"); + expect(repeatedStr).toEqual("hellohellohellohello"); }); -// case: handle Count of 1: + +// case: Handle Count of 1 : // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +// Then it should return the original str without repetition. +test("should return the original string when count is 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hello"); +}); -// case: Handle Count of 0: -// Given a target string str and a count equal to 0, -// When the repeat function is called with these inputs, -// Then it should return an empty string, ensuring that a count of 0 results in an empty output. +// case: Handle Count of 0 : +// When count is 0, it should return an empty string. +test("should return an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); -// case: Negative Count: -// Given a target string str and a negative integer count, -// When the repeat function is called with these inputs, -// Then it should throw an error or return an appropriate error message, as negative counts are not valid. +// case: Negative Count : +// When count is negative, it should throw an error. +test("should throw an error when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +}); + + +// case: Non-string input for str : +// When str is not a string (e.g., number), it should still repeat it as a string. +test("should convert non-string input to string before repeating", () => { + const str = 123; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("123123"); +}); + +// case: Non-integer count : +// When count is a non-integer (e.g., 2.5), it should repeat only the integer part. +test("should handle non-integer count by truncating it", () => { + const str = "hi"; + const count = 2.5; + const repeatedStr = repeat(str, Math.floor(count)); + expect(repeatedStr).toEqual("hihi"); +}); + +// case: Empty string input : +// When str is empty, the result should always be empty, regardless of count. +test("should return an empty string when str is empty", () => { + const str = ""; + const count = 5; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); + +// case: Undefined or missing arguments : +// When arguments are missing or undefined, it should throw an error. +test("should handle undefined inputs gracefully", () => { + expect(() => repeat(undefined, 3)).toThrow("argument must be defined"); + +}); + +// case: Boolean inputs : +// When str or count are booleans, it should convert str to string and handle count normally. +test("should handle boolean values as inputs", () => { + const str = true; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("truetrue"); +});