-
-
Notifications
You must be signed in to change notification settings - Fork 220
West Midlands | 25 Sep ITP | Iswat Bello | Sprint 1 | Module Data Groups #829
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
Conversation
… about arrays as reference types
This implementation handles arrays with an odd length.
…t mutate the array
…t median for an even-length array
…ulating the median
- Adjusted indentation and arrow function formatting - Added consistent line breaks and spacing - No changes to test logic or order
… to the `validNumbers`
…hen input has no duplicates
… occurrence for arrays of strings and numbers
…ng-number array handling
…turning a new array
cjyuan
left a comment
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.
Can you check if any of this general feedback can help you further improve your code?
https://github.com/CodeYourFuture/Module-Data-Groups/blob/sprint-1-feedback/Sprint-1/feedback.md
Doing so can help reviewers speed up the review process. Thanks.
Hi @cjyuan, thank you for sharing the general feedback. I’ll check it against my changes to see whether I’m on the right track. |
- Replaced `const copyArray = validNumbers.slice(); const sortArray = copyArray.sort(...)` with `const sorted = [...validNumbers].sort(...)`. - Created a shallow copy and sorts in one step. - Eliminated redundant `copyArray` variable while keeping original array intact.
- Added `expect(result).not.toBe(input)` to tests that check array output. - Confirms that the returned array is a new object, not the same reference as the input. - Maintains `toEqual` checks for content correctness. - Addresses feedback about verifying array copy vs original reference.
- Replaced `toBe` with `toBeCloseTo` for all tests involving decimal numbers. - Ensures floating-point sums are tested correctly despite precision issues. - Updated expected values to match actual sums when necessary (e.g., -7.4 instead of -7.399). - Keeps integer and exact sums using `toBe`.
Hi @cjyuan. I have made changes to some files based on the general feedback you shared. Thank you. |
cjyuan
left a comment
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.
Tests are very comprehensive! I am impressed.
Sprint-1/fix/median.js
Outdated
|
|
||
| // Create a shallow copy of `validNumbers` and sort it in ascending order | ||
| // This avoids modifying the original array | ||
| const sorted = [...validNumbers].sort((a, b) => a - b); |
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.
Do we need to clone validNumbers before sorting?
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.
Do we need to clone
validNumbersbefore sorting?
I have gone through the code and I can see that I do not need to clone validNumbers before sorting. I can mutate validNumbers directly since it is already a new array created by .filter(). I have updated the code with these changes.
| const result = dedupe(input); | ||
| expect(result).toEqual(expected); | ||
| expect(result).not.toBe(input); // ensures it's a new array |
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.
Nicely done.
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.
Nicely done.
Thank you.
Sprint-1/implement/max.test.js
Outdated
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("given an array with only non-number values", () => { | ||
| expect(findMax(["a", null, undefined])).toBe(-Infinity); | ||
| expect(findMax([{}, [], "hi"])); |
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.
Something is missing here
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.
Something is missing here
Thank you for pointing this out. I need to complete the test by adding the expected value.
Sprint-1/implement/sum.js
Outdated
| for (const item of filteredElements) { | ||
| sumElements += item; | ||
| } | ||
| return Number(sumElements.toFixed(2)); |
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 limit the result to only two decimal places?
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 limit the result to only two decimal places?
I used toFixed(2) earlier as a workaround for floating-point precision issues when the tests were using toBe, which checks for exact equality.
By rounding to two decimals, I could ensure the returned value matched the expected 0.44 style values.
Now that the tests use toBeCloseTo, the function no longer needs to round the result at all.
toBeCloseTo correctly handles floating-point precision, so I will remove the toFixed(2) rounding and return the raw sum.
Thank you for pointing this out, @cjyuan.
Thank you, @cjyuan. |
- Remove redundant cloning of validNumbers before sorting - Allow safe in-place sort since validNumbers is newly created from list - Update comments to allign with changes
… handle non-numeric arrays
cjyuan
left a comment
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.
Changes look good. Well done!
Sprint-1/fix/median.js
Outdated
|
|
||
| // Create a shallow copy of `validNumbers` and sort it in ascending order | ||
| // This avoids modifying the original array | ||
| const sorted = [...validNumbers].sort((a, b) => a - b); |
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 find sorted a more meaningful name for the array, you can also introduce it as
const sorted = validNumbers.sort((a, b) => a - b);
Sprint-1/implement/sum.js
Outdated
| sumElements += item; | ||
| } | ||
| return Number(sumElements.toFixed(2)); | ||
| return Number(sumElements); |
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.
sumElements is already a number.
Thank you so much @cjyuan for making the time to review my code and give detailed feedback. I really appreciate it. |
Learners, PR Template
Self checklist
Changelist
This pull request includes modifications to three directories:
Fix directory: I updated the function implementation so that it passes all test cases.
Implement directory: I added test cases based on the provided scenarios and implemented the function to handle all of them.
Refactor directory: I refactored the existing function implementation, ensuring it continues to pass all test cases.
Questions
Hi. Please could you review my PR? I’d really appreciate your feedback.