Repository navigation
Conversation
…ent exit in the string,test failed
mayowaolokun
left a comment
There was a problem hiding this comment.
Thank you for your work on this; it’s clear you’ve put real effort into the exercises. Replacing the hardcoded return values with working implementations is exactly what TDD is about, and it’s great to see you testing a wide range of inputs, including digits, symbols, spaces and error cases.
I’ve left a few comments to help with the next step.
You’re making good progress. Keep up the good work!
| return 5 | ||
| let count = 0; | ||
| for (let i = 0; i < stringOfCharacters.length; i++) { | ||
| if (stringOfCharacters[i] == findCharacter[0]) { |
There was a problem hiding this comment.
Comparing each character to findCharacter[0] works well when the target is a single character, as the exercise describes. It might be interesting to try passing something like "an" and see what happens. You could then decide whether the function should handle that case or whether it’s outside what the function promises, and add a test that documents your decision. A test for an empty string could also be a helpful way to confirm the behaviour you expect.
There was a problem hiding this comment.
Thank you for addressing this. Adding the length check makes the function’s contract clear, and throwing an error for invalid input is a good decision.
| test(" Should count multiple occurences of character", () => { | ||
| const str = "aaaaa"; | ||
| const char = "c"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); |
There was a problem hiding this comment.
Several of the test names are identical, and a few don’t quite match what the test checks. For example, line 15 checks for zero occurrences, while lines 22 and 28 check for one. Since the name is the first clue when a test fails, it might help to give each one a name that describes its exact scenario.
| @@ -1,14 +1,7 @@ | |||
| // implement a function countChar that counts the number of times a character occurs in a string | |||
| import { countChar } from "./count.js"; | |||
| // Given a string `str` and a single character `char` to search for, | |||
There was a problem hiding this comment.
The Given/When/Then comments you removed describe each scenario in plain language, so consider restoring them and writing your own for the new scenarios you’ve added.
There was a problem hiding this comment.
Thanks for the updates. The Given/When/Then comments still appear to have been removed. Restoring them would help document each scenario, so it might be worth adding them back before this is merged.
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(2); | ||
| }); |
There was a problem hiding this comment.
You can consider adding tests for an empty string and for a target longer than one character (like "an"), then decide whether the function should handle that or if it’s outside what the function promises.
There was a problem hiding this comment.
Thank you for adding both tests; this addresses the comment nicely.
| }); | ||
| test("should count multiple occurences of character", () => { | ||
| const str = "AAAa"; | ||
| const char = "a"; |
There was a problem hiding this comment.
Great idea to include this test. It shows that only the lowercase “a” is counted and the uppercase “A”s are ignored, so your function is case-sensitive. It might be helpful to rename the test to reflect that, perhaps by mentioning uppercase and lowercase.
There was a problem hiding this comment.
Thanks for renaming this; it’s much clearer. You could make it even more precise by saying that uppercase letters are not counted, so a reader knows the function is case-sensitive.
| const count = 0; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual(" "); |
There was a problem hiding this comment.
The original comment for this case mentioned that the function should return an empty string when count is 0. Could you take another look at what this test expects? It’s worth checking whether it matches the specification or the current behaviour of your code.
| // Given a target string `str` and a positive integer `count`, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should: | ||
|
|
||
| // 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. |
There was a problem hiding this comment.
The Given/When/Then comments you removed are the specifications for each case, so consider restoring them.
| test("should repeat the string count times", () => { | ||
| const str = "hello"; | ||
| const count = 2; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("hellohello"); | ||
| }); | ||
| test("should repeat the string count times", () => { | ||
| const str = "hello"; | ||
| const count = 4; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("hellohellohellohello"); | ||
| }); |
There was a problem hiding this comment.
All the tests currently share the name “should repeat the string count times”, which could make it hard to tell which one failed. It might help to name each test after the case it covers, for example, a count of 1, a count of 0 or a 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. | ||
|
|
There was a problem hiding this comment.
You can remove the extra blank line here
|
|
||
| test("should repeat the string count times", () => { | ||
| expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid"); | ||
| }); |
There was a problem hiding this comment.
It would be worth thinking about what should happen when str is an empty string. You might consider adding a test for that case, too.
There was a problem hiding this comment.
Thank you for adding this test. One thing to check: the input here is a single space " " rather than an empty string "". Please see my comment on line 9 of repeat-str.js, as the two are linked.
…given more than one character
… and added a test for empty string
…h a decimal input
mayowaolokun
left a comment
There was a problem hiding this comment.
Thank you for the updates, this is great progress. The main issues from the first review have been addressed: the 111, 112 and 113 cases now work, the zero case in repeatStr returns an empty string, and the test names are much clearer.
I’ve left a few smaller comments.
You’re very close, so keep up the good work!
| // When the function is called with these inputs, | ||
| // Then it should return 0, indicating that no occurrences of `char` were found. | ||
|
|
||
| test(" Should count 0", () => { |
There was a problem hiding this comment.
“Should count 0” is clearer than before. It might help to describe the scenario, for example, that the character doesn’t appear in the string. There’s also still a space at the start of the name.
| const count = countChar(str, char); | ||
| expect(count).toEqual(1); | ||
| }); | ||
| test("should count multiple occurences of character empty space", () => { |
There was a problem hiding this comment.
“Empty space” could be confused with an empty string. Would “space character” describe this test more clearly?
| const count = countChar(str, char); | ||
| expect(count).toEqual(2); | ||
| }); | ||
| test("should count multiple occurences of character in an empty string", () => { |
There was a problem hiding this comment.
Nice addition. Since this test expects 0, it might be worth removing “multiple occurrences” from the name.
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
| test("should count multiple occurences of character in a sentence", () => { |
There was a problem hiding this comment.
This test seems to share its name with the one on line 52, so it might be worth giving it a unique name that describes its scenario.
| throw new Error("Invalid Input"); | ||
| } | ||
|
|
||
| if (numStr.slice(-1) == "1" && numStr.slice(-2) != 11) { |
There was a problem hiding this comment.
numStr.slice(-2) gives you a string, but it’s compared with the number 11. This currently works because != converts types before comparing. What would happen if you switched to !==, as suggested earlier? It might be worth comparing like with like so the code doesn’t depend on type conversion. Applicable to lines 11 and 14.
| count = Math.floor(count); | ||
|
|
||
| if (str === " ") { | ||
| throw new Error("String shoild not be empty"); |
There was a problem hiding this comment.
Small typo: “shoild” should be “should”.
|
|
||
| test("should repeat the string count times", () => { | ||
| expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid"); | ||
| }); |
There was a problem hiding this comment.
Thank you for adding this test. One thing to check: the input here is a single space " " rather than an empty string "". Please see my comment on line 9 of repeat-str.js, as the two are linked.
| expect(() => repeatStr("hello", -1)).toThrow("Negative counts are not valid"); | ||
| }); | ||
| test(`should throw an error when string is empty`, () => { | ||
| expect(() => repeatStr(" ", 1)).toThrow("String shoild not be empty"); |
There was a problem hiding this comment.
The test name says “empty string”, but the input is a space. Once you’ve decided whether an empty string or a space should be an error, it might be worth updating this test to match. Also, if you fix the “shoild” typo in the error message, remember to update it here too.
|
|
||
| // 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. | ||
|
|
There was a problem hiding this comment.
The Given/When/Then comments for the count of 1 and count of 0 cases are still removed. It might be worth restoring them, as you’ve done for the other cases.
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
| //case: Handle decimal count: | ||
| test("should repeat the stringthe whole number in the count times", () => { |
There was a problem hiding this comment.
There’s a missing space in “stringthe”. It might also help to make the name clearer, for example, by saying that a decimal count is rounded down.
Learners, PR Template
Self checklist
Task code
CYF-1060
Changelist
I practiced writing a code based on Test driven development.
Questions