Skip to content

Commit 163ef9e

Browse files
Complete Sprint 3 practice TDD tasks
1 parent 3372770 commit 163ef9e

File tree

5 files changed

+144
-42
lines changed

5 files changed

+144
-42
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
/**
2+
* TDD Practice: Count Characters
3+
* -----------------------------
4+
* Goal: Implement a function that counts how many times a character appears in a string.
5+
* This version uses a classic 'for...of' loop for maximum clarity.
6+
*/
7+
18
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
9+
// 1. Initialize a counter starting from 0
10+
let totalCount = 0;
11+
12+
// 2. Loop through every character in the input string
13+
for (let currentCharacter of stringOfCharacters) {
14+
// 3. Check if the current character matches the character we are looking for
15+
if (currentCharacter === findCharacter) {
16+
// 4. If they match, increment the counter by 1
17+
totalCount++;
18+
}
19+
}
20+
21+
// 5. Return the final count after finishing the loop
22+
return totalCount;
323
}
424

25+
// Exporting the function so the test file can access it
526
module.exports = countChar;
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
1+
/**
2+
* getOrdinalNumber - Final Implementation
3+
* --------------------------------------
4+
* Converts a number to its English ordinal string (1st, 2nd, 3rd, etc.)
5+
*/
6+
17
function getOrdinalNumber(num) {
2-
return "1st";
8+
// 1. التعامل مع الاستثناءات (11, 12, 13)
9+
// نستخدم % 100 للحصول على آخر رقمين
10+
const lastTwoDigits = num % 100;
11+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
12+
return num + "th";
13+
}
14+
15+
// 2. الحصول على آخر رقم في العدد
16+
const lastDigit = num % 10;
17+
18+
// 3. تحديد النهاية بناءً على آخر رقم
19+
switch (lastDigit) {
20+
case 1:
21+
return num + "st";
22+
case 2:
23+
return num + "nd";
24+
case 3:
25+
return num + "rd";
26+
default:
27+
return num + "th";
28+
}
329
}
430

31+
// تصدير الدالة للاختبار
532
module.exports = getOrdinalNumber;
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
1-
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber.
3-
4-
// Continue testing and implementing getOrdinalNumber for additional cases.
5-
// Write your tests using Jest — remember to run your tests often for continual feedback.
1+
/**
2+
* getOrdinalNumber - Full Test Suite
3+
*/
64

7-
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
8-
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
9-
// Instead of writing tests for individual numbers, consider grouping all possible input values
10-
// into meaningful categories. Then, select representative samples from each category to test.
11-
// This approach improves coverage and makes our tests easier to maintain.
5+
const getOrdinalNumber = require("./get-ordinal-number");
126

137
// Case 1: Numbers ending with 1 (but not 11)
14-
// When the number ends with 1, except those ending with 11,
15-
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
8+
test("should append 'st' for numbers ending with 1", () => {
179
expect(getOrdinalNumber(1)).toEqual("1st");
1810
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
11+
expect(getOrdinalNumber(101)).toEqual("101st");
12+
});
13+
14+
// Case 2: Numbers ending with 2 (but not 12)
15+
test("should append 'nd' for numbers ending with 2", () => {
16+
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
expect(getOrdinalNumber(42)).toEqual("42nd");
18+
});
19+
20+
// Case 3: Numbers ending with 3 (but not 13)
21+
test("should append 'rd' for numbers ending with 3", () => {
22+
expect(getOrdinalNumber(3)).toEqual("3rd");
23+
expect(getOrdinalNumber(33)).toEqual("33rd");
24+
});
25+
26+
// Case 4: The 'Teens' exceptions (11, 12, 13)
27+
test("should append 'th' for 11, 12, and 13", () => {
28+
expect(getOrdinalNumber(11)).toEqual("11th");
29+
expect(getOrdinalNumber(12)).toEqual("12th");
30+
expect(getOrdinalNumber(13)).toEqual("13th");
31+
});
32+
33+
// Case 5: General 'th' cases
34+
test("should append 'th' for other numbers", () => {
35+
expect(getOrdinalNumber(4)).toEqual("4th");
36+
expect(getOrdinalNumber(10)).toEqual("10th");
2037
});
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
/**
2+
* repeatStr - Final Logic Implementation
3+
* -------------------------------------
4+
* This function handles all TDD cases:
5+
* 1. Multiple repetitions.
6+
* 2. Count of 1 (Returns original string).
7+
* 3. Count of 0 (Returns empty string).
8+
* 4. Negative count (Throws an Error).
9+
*/
10+
11+
function repeatStr(str, count) {
12+
// 1. شرط الحماية (Guard Clause):
13+
// نتحقق أولاً إذا كان الرقم سالباً قبل البدء بأي عملية.
14+
if (count < 0) {
15+
// إلقاء خطأ برمجي لإعلام نظام الاختبار أن المدخلات غير صحيحة
16+
throw new Error("Count must be a non-negative integer");
17+
}
18+
19+
// 2. استخدام دالة التكرار الجاهزة (repeat):
20+
// هذه الدالة ذكية جداً؛ فهي تكرر النص (str) بعدد مرات (count).
21+
// - إذا كان count يساوي 0، ستعيد نصاً فارغاً "" تلقائياً.
22+
// - إذا كان count يساوي 1، ستعيد النص كما هو.
23+
// - إذا كان count أكبر من 1، ستكرر النص.
24+
return str.repeat(count);
325
}
426

27+
// تصدير الدالة لكي يتمكن ملف الأختبار من الوصول إليها
528
module.exports = repeatStr;
Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
1-
// Implement a function repeatStr
2-
const repeatStr = require("./repeat-str");
3-
// Given a target string `str` and a positive integer `count`,
4-
// When the repeatStr function is called with these inputs,
5-
// Then it should:
1+
/**
2+
* repeatStr - Full Test Suite
3+
* --------------------------
4+
* These tests cover all the cases required by the assignment:
5+
* 1. Multiple repetitions.
6+
* 2. Count of 1.
7+
* 3. Count of 0.
8+
* 4. Negative count (Error handling).
9+
*/
610

7-
// Case: handle multiple repetitions:
8-
// Given a target string `str` and a positive integer `count` greater than 1,
9-
// When the repeatStr function is called with these inputs,
10-
// Then it should return a string that contains the original `str` repeated `count` times.
11+
const repeatStr = require("./repeat-str");
1112

12-
test("should repeat the string count times", () => {
13+
// Case 1: Handle multiple repetitions
14+
test("should repeat the string count times (e.g., 3 times)", () => {
1315
const str = "hello";
1416
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
17+
const result = repeatStr(str, count);
18+
expect(result).toEqual("hellohellohello");
1719
});
1820

19-
// Case: handle count of 1:
20-
// Given a target string `str` and a `count` equal to 1,
21-
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original `str` without repetition.
21+
// Case 2: Handle count of 1
22+
test("should return the original string without repetition when count is 1", () => {
23+
const str = "hello";
24+
const count = 1;
25+
const result = repeatStr(str, count);
26+
expect(result).toEqual("hello");
27+
});
2328

24-
// Case: Handle count of 0:
25-
// Given a target string `str` and a `count` equal to 0,
26-
// When the repeatStr function is called with these inputs,
27-
// Then it should return an empty string.
29+
// Case 3: Handle count of 0
30+
test("should return an empty string when count is 0", () => {
31+
const str = "hello";
32+
const count = 0;
33+
const result = repeatStr(str, count);
34+
expect(result).toEqual("");
35+
});
36+
37+
// Case 4: Handle negative count
38+
test("should throw an error when count is a negative integer", () => {
39+
const str = "hello";
40+
const count = -1;
2841

29-
// Case: Handle negative count:
30-
// Given a target string `str` and a negative integer `count`,
31-
// When the repeatStr function is called with these inputs,
32-
// Then it should throw an error, as negative counts are not valid.
42+
// Note: To test for errors in Jest, we wrap the function call in an anonymous function
43+
expect(() => {
44+
repeatStr(str, count);
45+
}).toThrow();
46+
});

0 commit comments

Comments
 (0)