generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 240
West Midlands | 25-ITP-Sep | Baba Yusuf | Sprint 3 | Coursework/sprint-3/2-practice-tdd #799
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
Baba05206
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
Baba05206: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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2ed214
checked npm install
Baba05206 b3f1041
Enhance countChar function with error handling and comprehensive test…
Baba05206 f84c03e
Update example outputs in countChar function for clarity
Baba05206 1570edd
Implement getOrdinalNumber function and add comprehensive test cases
Baba05206 bedc468
Refactor repeat function to handle input validation and add comprehen…
Baba05206 1092e07
Enhance countChar function with improved error handling and refactor …
e396395
Refactor countChar function for improved input validation and enhance…
Baba05206 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,36 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| // 1. Validate 'stringOfCharacters' input | ||
| if (stringOfCharacters === null || stringOfCharacters === undefined) { | ||
| throw new Error("The input string cannot be null or undefined."); | ||
| } | ||
| if (typeof stringOfCharacters !== "string") { | ||
| throw new Error("The input string must be a string type."); | ||
| } | ||
|
|
||
| // 2. Validate 'findCharacter' input | ||
| if (typeof findCharacter !== "string" || findCharacter.length !== 1) { | ||
| // This handles null, undefined, and strings that aren't a single character (including empty string "") | ||
| throw new Error( | ||
| "The character to count must be a single character string." | ||
| ); | ||
| } | ||
|
|
||
| // 3. Handle the empty string case (returns 0) | ||
| if (stringOfCharacters.length === 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| // 4. Perform the count | ||
| let count = 0; | ||
| for (let ch of stringOfCharacters) { | ||
| if (ch === findCharacter) { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
|
|
||
| // implement a function countChar that counts the number of times a character occurs in a string | ||
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,24 +1,101 @@ | ||
| // implement a function countChar that counts the number of times a character occurs in a string | ||
| const countChar = require("./count"); | ||
| // const countChar = require("./count"); | ||
| // 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'), | ||
| // 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'). | ||
|
|
||
| test("should count multiple occurrences of a character", () => { | ||
| const str = "aaaaa"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(5); | ||
| const countChar = require("./count"); | ||
|
|
||
| describe("countChar - Standard character counting", () => { | ||
| test.each([ | ||
| ["aaaaa", "a", 5], | ||
| ["hello", "l", 2], | ||
| ["javascript", "a", 2], | ||
| ["hello", "z", 0], | ||
| ["hello", "h", 1], | ||
| ["hipopotamos' make wonderful pets", "o", 4], | ||
| ["hipopotamos", "p", 2], | ||
| ["hipopotamos' are friendly animals", "t", 1], | ||
| ["hipopotamos", "x", 0], | ||
| ["Pneumonoultramicroscopicsilicovolcanoconiosis", "i", 6], | ||
| ])("should count occurrences of '%s' in '%s'", (input, char, expected) => { | ||
| expect(countChar(input, char)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("countChar - Edge cases", () => { | ||
| test("should return 0 for empty string", () => { | ||
| expect(countChar("", "a")).toBe(0); | ||
| }); | ||
|
|
||
| test("should return 0 when character is not found", () => { | ||
| expect(countChar("abcdef", "z")).toBe(0); | ||
| }); | ||
|
|
||
| test("should handle special characters", () => { | ||
| expect(countChar("!@#$%^&*", "&")).toBe(1); | ||
| }); | ||
|
|
||
| test("should be case-sensitive", () => { | ||
| expect(countChar("AaAaA", "a")).toBe(2); | ||
| expect(countChar("AaAaA", "A")).toBe(3); | ||
| }); | ||
|
|
||
| test("should handle numeric characters", () => { | ||
| expect(countChar("123123123", "2")).toBe(3); | ||
| }); | ||
|
|
||
| test("should handle whitespace characters", () => { | ||
| expect(countChar("a b c d e", " ")).toBe(4); | ||
| }); | ||
| }); | ||
|
|
||
| // Scenario: No Occurrences | ||
| // Given the input string str, | ||
| // And a character char that does not exist within the case-sensitive 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. | ||
| describe("countChar - Input validation", () => { | ||
| test("should throw error if input string is null", () => { | ||
| expect(() => countChar(null, "a")).toThrow( | ||
| "The input string cannot be null or undefined." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if input string is undefined", () => { | ||
| expect(() => countChar(undefined, "a")).toThrow( | ||
| "The input string cannot be null or undefined." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if input string is not a string", () => { | ||
| expect(() => countChar(12345, "a")).toThrow( | ||
| "The input string must be a string type." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if findCharacter is null", () => { | ||
| expect(() => countChar("abc", null)).toThrow( | ||
| "The character to count must be a single character string." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if findCharacter is undefined", () => { | ||
| expect(() => countChar("abc", undefined)).toThrow( | ||
| "The character to count must be a single character string." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if findCharacter is not a string", () => { | ||
| expect(() => countChar("abc", 1)).toThrow( | ||
| "The character to count must be a single character string." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if findCharacter is an empty string", () => { | ||
| expect(() => countChar("abc", "")).toThrow( | ||
| "The character to count must be a single character string." | ||
| ); | ||
| }); | ||
|
|
||
| test("should throw error if findCharacter is more than one character", () => { | ||
| expect(() => countChar("abc", "ab")).toThrow( | ||
| "The character to count must be a single character string." | ||
| ); | ||
| }); | ||
| }); |
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,16 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| function getOrdinalNumber(input) { | ||
| const lastDigit = input % 10; | ||
| const lastTwoDigits = input % 100; | ||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { | ||
| return input + "th"; | ||
| } else if (lastDigit === 1) { | ||
| return input + "st"; | ||
| } else if (lastDigit === 2) { | ||
| return input + "nd"; | ||
| } else if (lastDigit === 3) { | ||
| return input + "rd"; | ||
| } else { | ||
| return input + "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
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,8 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str, count) { | ||
| if (typeof str !== "string") throw new Error("Input must be a string"); | ||
| if (typeof count !== "number" || count < 0) | ||
| throw new Error("Count must be a non-negative number"); | ||
| return str.repeat(count); | ||
| } | ||
|
|
||
| module.exports = repeat; |
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
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.
Why treat
nullandundefineddifferently from other non-string types?