-
-
Notifications
You must be signed in to change notification settings - Fork 244
West Midlands | Sept-ITP-25 | Mustaf Asani | Sprint 3 | Coursework/sprint 3 practice tdd #813
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?
West Midlands | Sept-ITP-25 | Mustaf Asani | Sprint 3 | Coursework/sprint 3 practice tdd #813
Conversation
…he function that checks for that
…get the ordinal numbers
…t takes in a string and count to create a new string
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let pattern = new RegExp(findCharacter, "g"); | ||
|
|
||
| let matchingChars = stringOfCharacters.match(pattern); | ||
|
|
||
| if (matchingChars === null) { | ||
| return 0; | ||
| } | ||
|
|
||
| return matchingChars.length; | ||
| } |
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.
Does your function returns the value you expect in the following function calls?
countChar("=^.^=", "^");
countChar("=^.^=", ".");
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.
changed the function and used a split method with the findCharacter string literal to avoid special characters like the ones above in regex.
| if (num === 1) { | ||
| return "1st"; | ||
| } else if (num === 2) { | ||
| return "2nd"; | ||
| } else if (num === 3) { | ||
| console.log(num); | ||
| return "3rd"; | ||
| } else if (num > 3 || num < 21) { | ||
| return `${num}th`; | ||
| } |
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.
-
For best practices, code submitted to review should be free of debugging code (to keep the code clean).
-
What should the ordinal numbers of 103, 213, 31 be?
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.
made the function more robust so it can cover more digits and added more tests for different numbers
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.
also removed the use of individual checkers to allow for use with all numbers.
| test("should return '2nd' for 2", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| }); | ||
|
|
||
| test("should return '3rd' for 3", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); |
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.
To ensure thorough testing, we need broad scenarios that cover all possible cases.
Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories.
Then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.
For example, we can prepare a test for numbers 2, 22, 132, etc. as
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect( getOrdinalNumber(2) ).toEqual("2nd");
expect( getOrdinalNumber(22) ).toEqual("22nd");
expect( getOrdinalNumber(132) ).toEqual("132nd");
});
Can you make the tests in this script more comprehensive?
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.
added more tests in each suite to make the tests look at a wide variety of numbers
Sprint-3/2-practice-tdd/repeat.js
Outdated
| if (count === 0) { | ||
| return newStr; | ||
| } else if (count === 1) { | ||
| return (newStr += str); |
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.
Why not just return str directly?
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.
returned string directly only when count = 1
| } else { | ||
| return "Error invalid count used, please use integers from 0 upwards."; | ||
| } |
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.
How would the caller distinguish the result of the following two function calls?
repeat("Error invalid count used, please use integers from 0 upwards.", 1)repeat("", -1)
Both function calls return the same value.
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.
If the input is invalid (negative integers), how can we tell the user that there's an issue instead of returning a response?
| expect(repeatedStr).toEqual( | ||
| "Error invalid count used, please use integers from 0 upwards." | ||
| ); |
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.
If you modified repeat() to throw an error when count is negative, and you wanted to test if the function can throw an error as expected, you can use .toThrow(). You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)
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.
used to throw function to test if an invalid value had been input.
…s to count test file
…sts to check that different numbers work
|
Did you forget to push the changes to GitHub? |
| } else { | ||
| return "Error invalid count used, please use integers from 0 upwards."; | ||
| } |
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.
If the input is invalid (negative integers), how can we tell the user that there's an issue instead of returning a response?
| return "3rd"; | ||
| } else if (num > 3 || num < 21) { | ||
| return `${num}th`; | ||
| const lastDigit = num.toString()[num.toString().length - 1]; |
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 consider using the more efficient approach involving the % operator to extract the last digit and the last two digits from a number directly.
| test("should append 'st' to numbers with 1 at the end except for those ending with 11", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(101)).toEqual("101st"); | ||
| expect(getOrdinalNumber(151)).toEqual("151st"); | ||
| expect(getOrdinalNumber(2061)).toEqual("2061st"); | ||
| }); | ||
|
|
||
| test("should return '2nd' for 2", () => { | ||
| test("should append 'nd' to numbers with 2 at the end except for those ending with 12", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| expect(getOrdinalNumber(342)).toEqual("342nd"); | ||
| expect(getOrdinalNumber(592)).toEqual("592nd"); | ||
| expect(getOrdinalNumber(1972)).toEqual("1972nd"); | ||
| }); | ||
|
|
||
| test("should return '3rd' for 3", () => { | ||
| test("should append 'rd' to numbers with 3 at the end except for those ending with 13", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| expect(getOrdinalNumber(33)).toEqual("33rd"); | ||
| expect(getOrdinalNumber(353)).toEqual("353rd"); | ||
| expect(getOrdinalNumber(93)).toEqual("93rd"); | ||
| expect(getOrdinalNumber(783)).toEqual("783rd"); | ||
| }); | ||
|
|
||
| test("should return any number between 4 and 20 with a suffix of 'th' at end of number", () => { | ||
| test("should append 'th' to all other numbers which do not end in 1,2,3,11,12 or 13", () => { | ||
| expect(getOrdinalNumber(10)).toEqual("10th"); | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(212)).toEqual("212th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
| expect(getOrdinalNumber(17)).toEqual("17th"); | ||
| }); |
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.
These tests are quite comprehensive. If you use this test script to test your implementation, you can discover some bug in your code.
| ); | ||
| //return "Error invalid count used, please use integers from 0 upwards."; | ||
| } | ||
| return true; |
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.
-
What's the purpose of the statement on line 19? When do you expect it to be executed?
-
You can also consider structuring your code in the following way:
if (code < 0)
throw ...
if (code == 0) return "";
if (code == 1) return ...;
let newStr = "";
...
return newStr;
or (if you don't mind trading performance for simpler code)
if (count < 0)
throw ...;
// this code will produce the correct result for any count >= 0 anyway.
let newStr = "";
...
return newstr;
Learners, PR Template
Self checklist
Changelist
Created tests to check the expected output of functions before they are created.