Skip to content

Develop#2451

Open
Tanya-sktr wants to merge 3 commits intomate-academy:masterfrom
Tanya-sktr:develop
Open

Develop#2451
Tanya-sktr wants to merge 3 commits intomate-academy:masterfrom
Tanya-sktr:develop

Conversation

@Tanya-sktr
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
const compareByDefault = (a, b) => (String(a) > String(b) ? 1 : -1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


[].__proto__.sort2 = function (callback = compareByDefault) {
for (let j = 0; j < this.length; j++) {
for (let i = 0; i < this.length; i++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. Rate AI review example

const compareByDefault = (a, b) => (String(a) > String(b) ? 1 : -1);

[].__proto__.sort2 = function (callback = compareByDefault) {
for (let j = 0; j < this.length; j++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants