From f4e94174d6eb6464d666b1a9fe96eb41c16bcecf Mon Sep 17 00:00:00 2001 From: Mohsen Zamani Date: Wed, 28 Jan 2026 13:14:18 +0000 Subject: [PATCH] Coursework 2-practice-tdd --- Sprint-3/2-practice-tdd/count.js | 4 +- Sprint-3/2-practice-tdd/count.test.js | 21 ++++++--- Sprint-3/2-practice-tdd/get-ordinal-number.js | 19 +++++++- .../2-practice-tdd/get-ordinal-number.test.js | 47 ++++++++++++++++--- Sprint-3/2-practice-tdd/repeat-str.js | 5 +- Sprint-3/2-practice-tdd/repeat-str.test.js | 42 ++++++++++++----- 6 files changed, 107 insertions(+), 31 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..53e6316a8 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,7 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + return stringOfCharacters + .split("") + .reduce((acc, curr) => acc + (curr === findCharacter ? 1 : 0), 0); } 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..2948c64af 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,14 +1,14 @@ // implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); -// Given a string str and a single character char to search for, +// Given a string `str` and a single character `char` to search for, // When the countChar function is called with these inputs, // Then it should: // Scenario: Multiple Occurrences -// Given the input string str, -// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'), +// Given the input string `str`, +// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), // When the function is called with these inputs, -// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa'). +// Then it should correctly count occurrences of `char`. test("should count multiple occurrences of a character", () => { const str = "aaaaa"; @@ -18,7 +18,14 @@ test("should count multiple occurrences of a character", () => { }); // Scenario: No Occurrences -// Given the input string str, -// And a character char that does not exist within the case-sensitive str, +// 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 the char were found in the case-sensitive str. + +// Then it should return 0, indicating that no occurrences of `char` were found. +test("should return 0, since there are no occurrences of a character", () => { + const char = "a"; + const str = "AABBFFSAA"; + 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..84477418e 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,22 @@ function getOrdinalNumber(num) { - return "1st"; + if ([11, 12, 13].includes(num)) return `${num}th`; + const lastDigit = String(num).slice(-1); + const restOfNum = String(num).slice(0, -1); + let ordinalResult = ""; + switch (lastDigit) { + case "1": + ordinalResult = restOfNum + "1st"; + break; + case "2": + ordinalResult = restOfNum + "2nd"; + break; + case "3": + ordinalResult = restOfNum + "3rd"; + break; + default: + ordinalResult = restOfNum + lastDigit + "th"; + } + return ordinalResult; } 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 dfe4b6091..92416780e 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,46 @@ const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber +// In this week's prep, we started implementing getOrdinalNumber. -// continue testing and implementing getOrdinalNumber for additional cases -// Write your tests using Jest - remember to run your tests often for continual feedback +// Continue testing and implementing getOrdinalNumber for additional cases. +// Write your tests using Jest — remember to run your tests often for continual feedback. -// Case 1: Identify the ordinal number for 1 -// When the number is 1, -// Then the function should return "1st" +// To ensure thorough testing, we need broad scenarios that cover all possible cases. +// Listing individual values, however, can quickly lead to an unmanageable number of test cases. +// Instead of writing tests for individual numbers, consider grouping all possible input values +// into meaningful categories. Then, select representative samples from each category to test. +// This approach improves coverage and makes our tests easier to maintain. -test("should return '1st' for 1", () => { +// Case 1: Numbers ending with 1 (but not 11) +// When the number ends with 1, except those ending with 11, +// Then the function should return a string by appending "st" to the number. +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(31)).toEqual("131st"); +}); + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); +test("should return '5th' for 5", () => { + expect(getOrdinalNumber(5)).toEqual("5th"); +}); +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); +test("should return '21st' for 21", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); +}); +test("should return '32nd' for 32", () => { + expect(getOrdinalNumber(32)).toEqual("32nd"); +}); +test("should return '53rd' for 53", () => { + expect(getOrdinalNumber(53)).toEqual("53rd"); }); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..2e224ff02 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,6 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) throw new Error("Can not repeat negative times."); + return str.repeat(count); } 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 fc59d019e..59e84716f 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,13 +1,13 @@ // Implement a function repeatStr const repeatStr = require("./repeat-str"); -// Given a target string str and a positive integer count, +// Given a target string `str` and a positive integer `count`, // When the repeatStr function is called with these inputs, // Then it should: -// case: repeat String: -// Given a target string str and a positive integer count, +// Case: handle multiple repetitions: +// Given a target string `str` and a positive integer `count` greater than 1, // When the repeatStr function is called with these inputs, -// Then it should repeat the str count times and return a new string containing the repeated str values. +// Then it should return a string that contains the original `str` repeated `count` times. test("should repeat the string count times", () => { const str = "hello"; @@ -16,17 +16,33 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); -// case: handle Count of 1: -// Given a target string str and a count equal to 1, +// 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, ensuring that a count of 1 results in no repetition. +test("should repeat the string count times", () => { + 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, +// 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, ensuring that a count of 0 results in an empty output. +// Then it should return an empty string. +test("should repeat the string count times", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); -// case: Negative Count: -// Given a target string str and a negative integer count, +// 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 or return an appropriate error message, as negative counts are not valid. +// Then it should throw an error, as negative counts are not valid. +test("should repeat the string count times", () => { + const str = "hello"; + const count = -3; + expect(() => repeatStr(str, count)).toThrow("Can not repeat negative times."); +});