-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Solution #2472
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: master
Are you sure you want to change the base?
Solution #2472
Changes from all commits
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 |
|---|---|---|
|
|
@@ -4,9 +4,36 @@ | |
| * Implement method Sort | ||
| */ | ||
| function applyCustomSort() { | ||
| [].__proto__.sort2 = function(compareFunction) { | ||
| // write code here | ||
| [].__proto__.sort2 = function (compareFunction) { | ||
| if ( | ||
| compareFunction !== undefined && | ||
| typeof compareFunction !== 'function' | ||
| ) { | ||
| throw new TypeError( | ||
| 'The comparison function must be either a function or undefined', | ||
| ); | ||
| } | ||
|
|
||
| for (let i = 0; i < this.length; i++) { | ||
| for (let o = 0; o < this.length - 1 - i; o++) { | ||
|
Comment on lines
+17
to
+18
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. Using a simple bubble sort here is allowed for the task, but it's O(n²) and will perform poorly on large arrays. Consider mentioning this limitation or replacing it with a more efficient algorithm (quicksort/mergesort) if performance matters.
Comment on lines
+17
to
+18
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. This implementation uses bubble sort (the nested loops starting here). That's acceptable for the task, but consider adding a short comment noting the O(n²) performance so future readers understand the limitation and why a more efficient algorithm (e.g., quicksort or mergesort) would be preferable in production. |
||
| let shouldSwap; | ||
|
Comment on lines
+17
to
+19
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. Consider adding a short comment above these nested loops noting that this is a bubble sort (O(n²)) — it's acceptable for this task, but helps future readers understand the performance trade-off. |
||
|
|
||
| if (typeof compareFunction === 'function') { | ||
| shouldSwap = compareFunction(this[o], this[o + 1]) > 0; | ||
| } else { | ||
| shouldSwap = String(this[o]) > String(this[o + 1]); | ||
| } | ||
|
Comment on lines
+21
to
+25
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. If a |
||
|
|
||
| if (shouldSwap) { | ||
| [this[o], this[o + 1]] = [this[o + 1], this[o]]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return this; | ||
|
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. You define
so calls to |
||
| }; | ||
| /* eslint-disable-next-line no-extend-native */ | ||
| Array.prototype.sort = [].__proto__.sort2; | ||
| } | ||
|
|
||
| module.exports = applyCustomSort; | ||
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.
This creates
[].__proto__.sort2, but the task requires implementingArray.prototype.sortusing that predefinedsort2. The file never assigns or implementsArray.prototype.sort. Consider adding a line such asArray.prototype.sort = [].__proto__.sort2;(or similar) after definingsort2so the built-in method is implemented.