javascript-tutorial / javascript-tutorial/en.javascript.info

Lacking arguments in array.sort()'s compareFn

Đang mở
#3,562 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
HTML
Star
25.5k
Fork
4k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hello, I found this weird usage in test [Shuffle an array](https://javascript.info/array-methods#shuffle-an-array)'s "simple solution":

![image](https://github.com/javascript-tutorial/en.javascript.info/assets/80401151/678c9a62-033f-40db-b9aa-2d7b57c62e3b)

There is just no arguments in the `array.sort()`'s `compareFn`:
```js
array.sort(() => Math.random() - 0.5);
```

But [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters) says if we use `compareFn`, the arguments of `compareFn` "Will never be undefined.":

![image](https://github.com/javascript-tutorial/en.javascript.info/assets/80401151/27735ec7-2e97-4fb4-8aee-f9b2fa2d7d76)

According to MDN, the `compareFn` must be used with arguments, i.e.
```js
array.sort((a, b) => Math.random() - 0.5);
```

If it is a typo indeed, please fix it at your convenience, thank you.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

Bắt đầu với phần “Shuffle an array” được liên kết trong issue và tìm trong mã nguồn của tutorial ví dụ array.sort() được hiển thị. Đối chiếu cách diễn đạt về compareFn với tài liệu MDN được tham chiếu, sau đó cập nhật ví dụ nếu đó là lỗi đánh máy và xác minh rằng tutorial hiển thị snippet đã được sửa.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.