-
-
Notifications
You must be signed in to change notification settings - Fork 337
Sheffield | 26-ITP-Jan | Mahmoud Shaabo | Sprint 3 | Practice TDD #1214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mahmoudshaabo1984
wants to merge
2
commits into
CodeYourFuture:main
Choose a base branch
from
mahmoudshaabo1984:coursework/sprint-3-practice-tdd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When a test fails with the message "... other numbers", it may be unclear what "other numbers" actually refers to. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could better update the description and include tests to cover "numbers ending with 11, 12, 13" and not just the three numbers, 11, 12, 13.