Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Great job setting up the sort2 method on the Array prototype! The overall structure is correct. However, this requires a change because there's a critical bug in your sorting logic that prevents it from working correctly. In src/arrayMethodSort.js, the inner loop's condition i < this.length causes it to access an index out of bounds on the last iteration, as this[i + 1] becomes undefined. This breaks the sort. Please adjust the loop condition to i < this.length - 1 to fix this. Once that's resolved, your implementation should pass!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| const compareByDefault = (a, b) => (String(a) > String(b) ? 1 : -1); |
There was a problem hiding this comment.
This default comparison function works for sorting, but a standard compare function should return 0 for equal values. Here, if String(a) is equal to String(b), the function returns -1. Consider adding an explicit check for equality.
src/arrayMethodSort.js
Outdated
|
|
||
| [].__proto__.sort2 = function (callback = compareByDefault) { | ||
| for (let j = 0; j < this.length; j++) { | ||
| for (let i = 0; i < this.length; i++) { |
There was a problem hiding this comment.
The condition i < this.length in this loop can cause an issue. On the last iteration, when i is this.length - 1, this[i + 1] will be undefined. You should adjust the loop condition to avoid accessing an element outside the array's bounds.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on this iteration! I am approving your solution. You've successfully addressed the critical out-of-bounds bug from the previous review, and your sort2 method now functions correctly and meets all task requirements. I've added a couple of optional suggestions in the comments for how you could optimize your bubble sort algorithm for better performance, but these are not required changes. Great job!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| const compareByDefault = (a, b) => (String(a) > String(b) ? 1 : -1); | ||
|
|
||
| [].__proto__.sort2 = function (callback = compareByDefault) { | ||
| for (let j = 0; j < this.length; j++) { |
There was a problem hiding this comment.
Your bubble sort implementation is correct. For a future improvement, consider that after each pass of this outer loop, the largest remaining element moves to its correct sorted position. You can optimize the algorithm by reducing the range of the inner loop in subsequent passes (e.g., i < this.length - 1 - j).
No description provided.