diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..6d200c527 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,10 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[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..f1edb7314 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,24 +1,49 @@ +// ═══════════════════════════════════════════════════════ +// 🎯 Personal Implementation: countChar Function Tests + +// Purpose: Test-first practice for counting character occurrences +// Style: Gherkin-style BDD comments + Jest assertions +// ═══════════════════════════════════════════════════════ + // implement a function countChar that counts the number of times a character occurs in a string -const countChar = require("./count"); +//const countChar = require("./count"); + +// ─────────────────────────────────────────────────────── +// 📋 Specification: // 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 return the number of times `char` appears in `str`. +// ─────────────────────────────────────────────────────── + +// ═══════════════════════════════════════════════════════ +// 🧪 Test Scenarios +// ═══════════════════════════════════════════════════════ // Scenario: Multiple Occurrences +// ────────────────────────────── // 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 occurrences of `char`. - +const countChar = require("./count"); test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; - const char = "a"; + const str = "aaaaa"; // 🎯 Input: repeated character + const char = "a"; // 🔍 Search for: 'a' const count = countChar(str, char); - expect(count).toEqual(5); + expect(count).toEqual(5); // ✅ Expect: 5 occurrences }); // 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 for no occurence", () => { + const str = "asdf"; // 🎯 Input: no 'l' present + const char = "l"; // 🔍 Search for: 'l' (not in string) + const count = countChar(str, char); + expect(count).toEqual(0); // ✅ Expect: 0 occurrences +}); + + diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..2e3502dbe 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,14 @@ function getOrdinalNumber(num) { - return "1st"; -} + let numberToString = String(num); + let numberLastDigit = numberToString.slice(-1); + let numberLast2Digits = numberToString.slice(-2); + if (numberLastDigit === "1" && numberLast2Digits !== "11") + return numberToString + "st"; + if (numberLastDigit === "2" && numberLast2Digits !== "12") + return numberToString + "nd"; + if (numberLastDigit === "3" && numberLast2Digits !== "13") + return numberToString + "rd"; + return numberToString + "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 adfa58560..b6b1eb34b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,20 +1,47 @@ + +//const getOrdinalNumber = require("./get-ordinal-number"); + +// ─────────────────────────────────────────────────────── +// 📋 Rule Summary: +// • 1 → "1st", 2 → "2nd", 3 → "3rd" (unless teen) +// • 11, 12, 13 → always "th" (teen exception) +// • All others → last digit determines suffix +// ─────────────────────────────────────────────────────── const getOrdinalNumber = require("./get-ordinal-number"); -// 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. - -// 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. - -// 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(131)).toEqual("131st"); +// ── Case 1: Numbers ending in 1 (excluding teens) → "st" ── +test("appends 'st' to numbers ending in 1, except 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); // single digit + expect(getOrdinalNumber(21)).toEqual("21st"); // double digit + expect(getOrdinalNumber(131)).toEqual("131st");// triple digit +}); + +// ── Case 2: Numbers ending in 2 (excluding teens) → "nd" ── +test("appends 'nd' to numbers ending in 2, except 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(122)).toEqual("122nd"); +}); + +// ── Case 3: Numbers ending in 3 (excluding teens) → "rd" ── +test("appends 'rd' to numbers ending in 3, except 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(123)).toEqual("123rd"); }); + +// ── Case 4: Default "th" suffix (teens + digits 0,4-9) ── +test("appends 'th' for teens (11-13) and digits 0,4-9", () => { + // Teen exceptions (critical edge case) + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); // larger teen + + // Default suffix digits + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(25)).toEqual("25th"); +}); + + + diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..d7b507f92 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,5 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) throw new Error("negative counts are not valid"); + 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 a3fc1196c..0bc3e3505 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -1,32 +1,37 @@ -// Implement a function repeatStr -const repeatStr = require("./repeat-str"); -// Given a target string `str` and a positive integer `count`, -// When the repeatStr function is called with these inputs, -// Then it should: +//const repeatStr = require("./repeat-str"); -// 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 return a string that contains the original `str` repeated `count` times. +// ─────────────────────────────────────────────────────── +// Rule Summary: +// • count > 1 → str repeated count times ("hi", 3 → "hihihi") +// • count = 1 → original string unchanged +// • count = 0 → empty string "" +// • count < 0 → throw error (invalid input) +// ─────────────────────────────────────────────────────── +const repeatStr = require("./repeat-str"); +// ── Case 1: Multiple repetitions (count > 1) ── +test("repeats string correctly for count > 1", () => { + expect(repeatStr("hello", 3)).toEqual("hellohellohello"); + expect(repeatStr("a", 5)).toEqual("aaaaa"); + expect(repeatStr("xy", 2)).toEqual("xyxy"); +}); -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); +// ── Case 2: Single repetition (count = 1) ── +test("returns original string unchanged when count = 1", () => { + expect(repeatStr("bye", 1)).toEqual("bye"); + expect(repeatStr("x", 1)).toEqual("x"); + expect(repeatStr("test", 1)).toEqual("test"); }); -// 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. +// ── Case 3: Zero repetition (count = 0) ── +test("returns empty string when count = 0", () => { + expect(repeatStr("no", 0)).toEqual(""); + expect(repeatStr("anything", 0)).toEqual(""); + expect(repeatStr("", 0)).toEqual(""); +}); -// 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. +// ── Case 4: Invalid input (negative count) ── +test("throws error for negative count values", () => { + expect(() => repeatStr("str", -1)).toThrow("negative counts are not valid"); + expect(() => repeatStr("x", -10)).toThrow("negative counts are not valid"); +}); -// 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.