Repository navigation
London | 26-ITP-Sept | Hugh Mills | Sprint 2 | practice tdd #1697
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
base: main
Are you sure you want to change the base?
Changes from all commits
e921f3c
5dfa1de
f920f8c
33059ce
126b724
e86b00a
380ae27
c031244
7ad95cf
f3d3d7c
f1b93ea
b15db6b
c8ec84e
641752b
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,3 +1,7 @@ | ||
| export function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| export function countChar(str, char) { | ||
| let charMatch = str.match(new RegExp(char, "g")); | ||
| if (charMatch === null) { | ||
| return 0; | ||
| } | ||
| return charMatch.length; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,45 @@ test("should count multiple occurrences of a character", () => { | |
| // And a character `char` that does not exist within `str`. | ||
| // 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 with no occurrences of a character", () => { | ||
| const str = "aaaaa"; | ||
| const char = "b"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
Comment on lines
+26
to
+31
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. There is a special
Author
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. Is that when str has nothing in it? I have added a test for this aswell as some others that have just thought of. |
||
|
|
||
| test("should count 0 with no empty string", () => { | ||
| const str = ""; | ||
| const char = "b"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character in random string", () => { | ||
| const str = "accababaade"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character in string with empty spaces", () => { | ||
| const str = "aa a aa"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character in string ignoring upper case", () => { | ||
| const str = "aaAaaa"; | ||
| const char = "a"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character in string ignoring lower case", () => { | ||
| const str = "aAAaAAA"; | ||
| const char = "A"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(5); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| export function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| let numString = num.toString(); | ||
| if (11 <= Number(numString.slice(-2)) && 19 >= Number(numString.slice(-2))) { | ||
| return numString + "th"; | ||
| } else if ( | ||
| -11 >= Number(numString.slice(-2)) && | ||
| -19 <= Number(numString.slice(-2)) | ||
| ) { | ||
|
Comment on lines
+3
to
+8
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. Please be aware of the consistency of code formatting - while the first if using one line for the conditions but the second if using multiple lines for the conditions. I personally would prefer one line which may usually be easier to read, unless explicitly specified by coding standard in a business environment.
Author
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. I am aware of that, this is the doing of the auto format that we had to put into vscode. I tried to keep it like line 3 and it just automatically does that format, when I saw it doing that I tried to make it then be like lines 6 and 7 but then it forces it back to how it was at 3. Not sure how to stop it doing that when I hit tell it to save. 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. Thanks for letting me know. I will discuss with cirriculum team to improve the auto format file. All good for now. |
||
| return numString + "th"; | ||
| } else if (numString.slice(-1) === "1") { | ||
| return numString + "st"; | ||
| } else if (numString.slice(-1) === "2") { | ||
| return numString + "nd"; | ||
| } else if (numString.slice(-1) === "3") { | ||
| return numString + "rd"; | ||
| } | ||
| return numString + "th"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| export function repeatStr() { | ||
| export function repeatStr(str, count) { | ||
| // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). | ||
| // The goal is to re-implement that function, not to use it. | ||
| return "hellohellohello"; | ||
| if (count < 0) { | ||
| throw new Error("negative counts are not valid"); | ||
| } | ||
| let result = ""; | ||
| for (let i = 0; i < count; i++) { | ||
| result += str; | ||
| } | ||
| return result; | ||
| } |
|
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. Your test cases are correct. Just for you to think a little bit more, besides
Author
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. Ah i think I understand, ones where it has a space or symbols, even an empty one. Added those tests. 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. Yes, sometimes space or symbols can be boundary cases. In the context of this program, only empty and non-empty string matters. Empty string with any positive or zero count should still be empty. However, empty string with negative count should still throw error. Some trainees also return empty string for negative count which would not be quite accurate "for the context of this question". This is why we need to test empty string
Author
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. Ah didn't think of the negatives there, need to remeber those. I see what you mean, I'll look into the mentored coding session I got coming up to ask about it but will look into it aswell. |
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.
Your code is correct. Just wondering what is the advantage of using this approach over other implementations? Thank you.
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.
Of course from what I understand, its when its more test driven it helps you build the code as your making tests for it, it helps keep a better flow and streamlined work on the code while addressing the tests at the same time. If you just build the code itself without tests in this way it can lead to unseen issues that end up rebuild large chunks of the code that can result in making more bugs and issues.
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.
Sorry maybe my original question was not clear. I didn't mean the advantages of TDD, I was wondering how would you come up with the following approach with the given test cases:
charMatch = str.match(new RegExp(char, "g"))charMatch === nullreturn charMatch.lengthEspecially what is the advantage of such implementation but not other programming logics? Thank you for your claification.
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 think it was when I was building up the code, I looked for something to use to count the "char" but i cant recall which i used but was only counting the first letter so when I searched for options on MDN i found .match() and used that one.
I found out about RegExp on the same page as .match() and saw it was used to help with finding the pattern of what i was searching for.
I still need to look into it more to understand it better to be honest.
If i think of it, this takes all the occurrences of g out of the string
For the null one I ran into an error that was showing null instead of 0 at the time so i think I put in an if to catch it out to show 0.
After finding those when I was stumped I couldn't think of any other logics I could have used at the time. I think i did it diffrently before so will look it up after this.
Uh oh!
There was an error while loading. Please reload this page.
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.
No worries, although the explanations are not quite accurate, at least I understand your thinking. Even this is not the best implementation, I would let you pass this one because the program runs correctly. Maybe try to book a mentored coding session with volunteer to try another way to solve the same exercise. Thank you.
p.s.: the
ginnew RegExp(char, "g")means "global search" but not searching for character "g".