Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/**
* TDD Practice: Count Characters
* -----------------------------
* Goal: Implement a function that counts how many times a character appears in a string.
* This version uses a classic 'for...of' loop for maximum clarity.
*/

function countChar(stringOfCharacters, findCharacter) {
return 5
// 1. Initialize a counter starting from 0
let totalCount = 0;

// 2. Loop through every character in the input string
for (let currentCharacter of stringOfCharacters) {
// 3. Check if the current character matches the character we are looking for
if (currentCharacter === findCharacter) {
// 4. If they match, increment the counter by 1
totalCount++;
}
}

// 5. Return the final count after finishing the loop
return totalCount;
}

// Exporting the function so the test file can access it
module.exports = countChar;
29 changes: 28 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
/**
* getOrdinalNumber - Final Implementation
* --------------------------------------
* Converts a number to its English ordinal string (1st, 2nd, 3rd, etc.)
*/

function getOrdinalNumber(num) {
return "1st";
// 1. التعامل مع الاستثناءات (11, 12, 13)
// نستخدم % 100 للحصول على آخر رقمين
const lastTwoDigits = num % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return num + "th";
}

// 2. الحصول على آخر رقم في العدد
const lastDigit = num % 10;

// 3. تحديد النهاية بناءً على آخر رقم
switch (lastDigit) {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}

// تصدير الدالة للاختبار
module.exports = getOrdinalNumber;
45 changes: 31 additions & 14 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
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.
/**
* getOrdinalNumber - Full Test Suite
*/

// 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.
const getOrdinalNumber = require("./get-ordinal-number");

// 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", () => {
test("should append 'st' for numbers ending with 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(101)).toEqual("101st");
});

// Case 2: Numbers ending with 2 (but not 12)
test("should append 'nd' for numbers ending with 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(42)).toEqual("42nd");
});

// Case 3: Numbers ending with 3 (but not 13)
test("should append 'rd' for numbers ending with 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
});

// Case 4: The 'Teens' exceptions (11, 12, 13)
test("should append 'th' for 11, 12, and 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 5: General 'th' cases
test("should append 'th' for other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
});
27 changes: 25 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
function repeatStr() {
return "hellohellohello";
/**
* repeatStr - Final Logic Implementation
* -------------------------------------
* This function handles all TDD cases:
* 1. Multiple repetitions.
* 2. Count of 1 (Returns original string).
* 3. Count of 0 (Returns empty string).
* 4. Negative count (Throws an Error).
*/

function repeatStr(str, count) {
// 1. شرط الحماية (Guard Clause):
// نتحقق أولاً إذا كان الرقم سالباً قبل البدء بأي عملية.
if (count < 0) {
// إلقاء خطأ برمجي لإعلام نظام الاختبار أن المدخلات غير صحيحة
throw new Error("Count must be a non-negative integer");
}

// 2. استخدام دالة التكرار الجاهزة (repeat):
// هذه الدالة ذكية جداً؛ فهي تكرر النص (str) بعدد مرات (count).
// - إذا كان count يساوي 0، ستعيد نصاً فارغاً "" تلقائياً.
// - إذا كان count يساوي 1، ستعيد النص كما هو.
// - إذا كان count أكبر من 1، ستكرر النص.
return str.repeat(count);
}

// تصدير الدالة لكي يتمكن ملف الأختبار من الوصول إليها
module.exports = repeatStr;
62 changes: 38 additions & 24 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
// 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:
/**
* repeatStr - Full Test Suite
* --------------------------
* These tests cover all the cases required by the assignment:
* 1. Multiple repetitions.
* 2. Count of 1.
* 3. Count of 0.
* 4. Negative count (Error handling).
*/

// 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.
const repeatStr = require("./repeat-str");

test("should repeat the string count times", () => {
// Case 1: Handle multiple repetitions
test("should repeat the string count times (e.g., 3 times)", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
const result = repeatStr(str, count);
expect(result).toEqual("hellohellohello");
});

// 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 2: Handle count of 1
test("should return the original string without repetition when count is 1", () => {
const str = "hello";
const count = 1;
const result = repeatStr(str, count);
expect(result).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.
// Case 3: Handle count of 0
test("should return an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const result = repeatStr(str, count);
expect(result).toEqual("");
});

// Case 4: Handle negative count
test("should throw an error when count is a negative integer", () => {
const str = "hello";
const count = -1;

// 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.
// Note: To test for errors in Jest, we wrap the function call in an anonymous function
expect(() => {
repeatStr(str, count);
}).toThrow();
});