Repository navigation
Conversation
…ent exit in the string,test failed
…ounts is negative
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.
| 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.
| 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.
| }); | ||
| 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.
| 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.
Learners, PR Template
Self checklist
Task code
CYF-1060
Changelist
I practiced writing a code based on Test driven development.
Questions