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
35 changes: 19 additions & 16 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
47 changes: 45 additions & 2 deletions src/arrayMethodSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,52 @@
* Implement method Sort
*/
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
// write code here
[].__proto__.sort2 = function (compareFunction) {

Choose a reason for hiding this comment

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

You define [].__proto__.sort2 here, but the task requires implementing Array.prototype.sort using the predefined sort2. After defining sort2, assign it to the standard sort so calls to arr.sort() use your implementation (for example, set Array.prototype.sort to the sort2 implementation).

if (
compareFunction !== undefined &&
typeof compareFunction !== 'function'
) {
throw new TypeError(
'The comparison function must be either a function or undefined',
);
Comment on lines +12 to +14

Choose a reason for hiding this comment

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

The thrown TypeError message text is currently: The comparison function must be either a function or undefined. Tests and the required behavior expect the exact message compareFunction must be a function when a non-function (but not undefined) comparator is passed. Change the message to compareFunction must be a function to match expected behavior.

}

let swapped;

do {
swapped = false;

Comment on lines +19 to +21

Choose a reason for hiding this comment

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

Optional suggestion: add a short comment that this implementation uses bubble sort (O(n²)) and that more efficient algorithms (e.g. quicksort/mergesort) are preferable for large arrays. This is informational only and not required by the task.

for (let i = 0; i < this.length - 1; i++) {
const a = this[i];
const b = this[i + 1];
let shouldSwap = false;

if (compareFunction) {

Choose a reason for hiding this comment

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

Using if (compareFunction) treats any falsy value (e.g. null, 0) as if no comparator was provided. Check that the comparator is actually callable instead. For example: if (compareFunction !== undefined && typeof compareFunction !== 'function') throw new TypeError('compareFunction must be a function'); const comparator = typeof compareFunction === 'function' ? compareFunction : (a, b) => String(a) > String(b) ? 1 : (String(a) < String(b) ? -1 : 0);

if (compareFunction(a, b) > 0) {
shouldSwap = true;
}
} else {
Comment on lines +27 to +31

Choose a reason for hiding this comment

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

You use if (compareFunction) to decide whether to call the custom comparator. Although non-function values are checked above, it's clearer and more correct to use if (typeof compareFunction === 'function') to make explicit that only functions are treated as comparators (this mirrors the spec and avoids subtle falsy-value issues).

if (String(a) > String(b)) {
shouldSwap = true;
}
}

if (shouldSwap) {
const temp = this[i];

this[i] = this[i + 1];

this[i + 1] = temp;

swapped = true;
}
}
} while (swapped);

return this;
};

[].__proto__.sort = [].__proto__.sort2;
}

module.exports = applyCustomSort;