-
-
Notifications
You must be signed in to change notification settings - Fork 337
Manchester | 26-ITP-Jan | Mehroz Munir | Sprint 3 | Practice TDD Coursework #1147
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 1 commit
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,8 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| if (findCharacter === "") | ||
| return 0; | ||
| else | ||
| return stringOfCharacters.split(findCharacter).length -1; | ||
|
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. interesting use of
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. When I was thinking about the solutions how can i count the number of characters in a given string. The first thing came to my mind was the use of some loop to go through each character of string and compare it with the character i am looking for and add 1 to your count. But, then I realized that I can solve it more efficiently without using loop with the use of split method as through split I can get an array of strings separated by character i am looking for in the separater parameter. So, if I have five occurences of my character in the string, it will return my an array of size 6 that would be strings separated by that character including the empty strings. 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. Very good, nice innovative solution |
||
| } | ||
|
|
||
| module.exports = countChar; | ||
| module.exports = countChar; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| let numberString = String(num); | ||
| lastTwoCharactersNumberString = numberString.slice(-2); | ||
| lastCharacterNumberString = numberString.slice(-1); | ||
| if(lastTwoCharactersNumberString === "11") | ||
|
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 check your formatting in this file.
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 sorry my mistake. I had installed prettier for formatting, but did not enable it in the settings as the default formatter and use it on save. I have done it now. And it looks much better. Thank you. |
||
| return num+ "th"; | ||
| else if(lastCharacterNumberString === "1") | ||
| return num+ "st"; | ||
| else if(lastCharacterNumberString === "2") | ||
| return num+"nd"; | ||
| else if(lastCharacterNumberString === "3") | ||
| return num + "rd"; | ||
| else | ||
| return num + "th"; | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| function repeatStr() { | ||
| return "hellohellohello"; | ||
| function repeatStr(str, count) { | ||
| try{ | ||
|
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 check your formatting in this file. |
||
| if(count === 0) | ||
| return ""; | ||
| else if(count === 1) | ||
| return str; | ||
| else if(count < 0) | ||
| throw new Error("an error is thrown"); | ||
| else | ||
| return str.repeat(count); | ||
| } | ||
| catch(e){ | ||
|
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. why are you wrapping this in a
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 was wrapping it because i was catching the thrown error in the catch block and send the error message string back so that I will be able to compare this string in my test file and would know that the error actually is being thrown. But, I just have learned about the better way how can I test errors thrown in my test file while I was working on a different part of this module. And for that I don't need try catch block here. I have implement this change in the code. Thanks. 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. Okay awesome.
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. Yes, thank you for the advice. I totally agree with that. I have updated the error message now. Thanks. |
||
| return e.message; | ||
| } | ||
| } | ||
|
|
||
| module.exports = repeatStr; | ||
| module.exports = repeatStr; | ||
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.
good validation checking. is there perhaps another parameter here that could result in the same output?
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.
Yes I think that I can check the length of the findCharacter here and if that is 0 I would return 0;
if (findCharacter.length === 0) return 0;And I think that is a better validation than checking if string is empty. I have replaced this in my code now.
Thanks
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.
While that is true I was actually referring to something else.
Look at the method again and see
is there perhaps another parameter here that could result in the same output?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.
oh yes. Actually, if
stringOfCharacters.length === 0, I am expecting the same result. I have added it to my code now.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.
@MehrozMunir 100% yes! defensive coding is always a good idea