Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,37 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You define the sort2 method on [].__proto__ here, but the task requires implementing Array.prototype.sort using that predefined sort2. Currently there is no assignment that overrides Array.prototype.sort, so tests expecting a custom sort will fail.

const compare =
compareFunction ||
function (a, b) {
const A = String(a);
const B = String(b);

if (A > B) {
return 1;
}

if (A < B) {
return -1;
}

return 0;
};

for (let i = 1; i < this.length; i++) {
const current = this[i];
let j = i - 1;

while (j >= 0 && compare(this[j], current) > 0) {
this[j + 1] = this[j];
j--;
}

this[j + 1] = current;
}

return this;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After the sort2 implementation you should assign Array.prototype.sort to use it, e.g. Array.prototype.sort = function(compare) { return this.sort2(compare); };. Also ensure applyCustomSort() is called (or tests will call it) so the methods are actually installed.

};
}

Expand Down
11 changes: 11 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>arrayMethodSort</title>
</head>
<body>
<script src="./arrayMethodSort.js"></script>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The page only loads arrayMethodSort.js but does not invoke the exported applyCustomSort() installer. As a result your custom sort2/sort will not be installed — call applyCustomSort() (or modify the module to call it on load) so the prototype changes take effect before any sorting is done.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This line loads arrayMethodSort.js directly in the browser. The implementation file uses CommonJS (module.exports) and exports an installer function instead of running on load, which can cause ReferenceError: module is not defined and will not install the custom sort. Consider bundling the file for the browser, exposing and calling applyCustomSort() from a global, or converting the implementation to an ES module and importing it so the installer runs.

</body>
</html>