Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
51 changes: 49 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,55 @@
* 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.

if (
compareFunction !== undefined &&
typeof compareFunction !== 'function'
) {
throw new TypeError(
'The comparison function must be either a function or undefined',
);
}

const compare =
compareFunction ||
((a, b) => {
const s1 = String(a);
const s2 = String(b);

if (s1 < s2) {
return -1;
}

if (s1 > s2) {
return 1;
}

return 0;
});

const length = this.length;

for (let i = 0; i < length; i++) {
let swapped = false;

for (let j = 0; j < length - 1 - i; j++) {
if (compare(this[j], this[j + 1]) > 0) {
[this[j], this[j + 1]] = [this[j + 1], this[j]];
swapped = true;
}
}

if (!swapped) {
break;
}
}

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.

};

[].__proto__.sort = 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 wired Array.prototype.sort to call sort2 here — good. However, applyCustomSort is only exported and never invoked, so the custom methods are not actually installed at runtime. Call applyCustomSort() during module initialization (for example, just before module.exports) or ensure the test setup calls this exported function so the prototype changes take effect.

return this.sort2(compareFunction);
};
}

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>