-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 25-ITP-September |Sophia Mohamed |Sprint 3 | 2 practice tdd #842
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
Changes from 10 commits
7312985
113d089
d8fd187
5c3f09f
2614707
c8c9bc1
620ee39
745b6e9
d6f14a8
45f409b
566278d
407bf05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| } | ||
| function countChar(str, char) { | ||
| let count = 0; | ||
|
|
||
| for (let i = 0; i < str.length; i++) {//loop through each character in the string | ||
| if (str[i] === char) {//check if the character matches the target character | ||
| count = count + 1; | ||
| } | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
| return count;//return the final count | ||
| } | ||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,29 @@ | ||
| // implement a function countChar that counts the number of times a character occurs in a string | ||
| 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: Single Occurrence | ||
|
|
||
| test("counts a single occurrence of a character", () => { | ||
| expect(countChar("hello", "h")).toEqual(1); | ||
| expect(countChar("world", "d")).toEqual(1); | ||
| }); | ||
| // 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("counts multiple occurrences of a character", () => { | ||
| expect(countChar("banana", "a")).toEqual(3); | ||
| expect(countChar("mississippi", "s")).toEqual(4); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character", () => { | ||
| const str = "aaaaa"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| const Char = "a"; | ||
| const count = countChar(str,Char); | ||
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
|
Comment on lines
+24
to
+30
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. To keep the code clean, why not delete all unused code? |
||
|
|
@@ -22,3 +32,11 @@ test("should count multiple occurrences of a character", () => { | |
| // 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. | ||
| // Scenario: No Occurrences | ||
| test("should return 0 if the character is not in the string", () => { | ||
| const str = "hello"; | ||
| const char = "x"; // character not present in str | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,21 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const lastTwoDigits = num % 100; // check the last 2 digits for 11,12,13 | ||
| const lastDigit = num % 10; | ||
|
|
||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { | ||
| return num + "th"; | ||
| } | ||
|
|
||
| switch (lastDigit) { | ||
| case 1: | ||
| return num + "st"; | ||
| case 2: | ||
| return num + "nd"; | ||
| case 3: | ||
| return num + "rd"; | ||
| default: | ||
| return num + "th"; | ||
| } | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,13 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| }); | ||
| test("should return 'th' for 11, 12, 13", () => { | ||
| expect(getOrdinalNumber(11)).toBe("11th"); | ||
| expect(getOrdinalNumber(12)).toBe("12th"); | ||
| expect(getOrdinalNumber(13)).toBe("13th"); | ||
| }); | ||
| test("should return correct ordinal for numbers > 20", () => { | ||
| expect(getOrdinalNumber(21)).toBe("21st"); | ||
| expect(getOrdinalNumber(22)).toBe("22nd"); | ||
| expect(getOrdinalNumber(23)).toBe("23rd"); | ||
| }); | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(word, times) { | ||
| if (times < 0) { | ||
| throw new Error("Count must be a non-negative number"); | ||
| } | ||
| return word.repeat(times); | ||
| } | ||
|
|
||
| module.exports = repeat; |
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 also merge this test into the previous test (they both test "multiple occurrences").
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.
I have updated the test