Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
60 changes: 57 additions & 3 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,63 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
};
const proto = Object.getPrototypeOf([]);

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

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;

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.

}

Object.defineProperty(proto, 'sort2', {
value: customSort,
writable: true,
configurable: true,
});

Object.defineProperty(proto, 'sort', {
value: function (compareFunction) {
return customSort.call(this, compareFunction);

Choose a reason for hiding this comment

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

Requirement: the task requires sort to be implemented using the predefined [].__proto__.sort2. Here sort calls customSort directly — change this so sort delegates to sort2. For example: return proto.sort2.call(this, compareFunction); or return this.sort2(compareFunction); so the implementation is based on the predefined sort2 method (matches the task spec).

},
writable: true,
configurable: true,
});
}

module.exports = applyCustomSort;

Choose a reason for hiding this comment

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

Critical: applyCustomSort is only exported and never invoked. Because the installer isn't called, sort2 and the overridden sort are not installed on Array.prototype at runtime. Call applyCustomSort() (for example add applyCustomSort(); after this definition) or ensure your entry/test code imports and invokes it before any sorting occurs. Also be aware that module.exports = ... won't work when the file is included directly in a browser (see src/index.html) — either bundle the module or expose+call the installer from a browser-compatible script.

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>

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.

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>