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

Lacking arguments in array.sort()'s compareFn

未關閉
#3,562 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
HTML
星號
25.5k
分支
4k
PR 合併指標
30 天內沒有已合併 PR

描述

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.

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

從 issue 中連結的「Shuffle an array」區段開始,在教學的原始碼中搜尋所顯示的 array.sort() 範例。對照所參照的 MDN 文件確認 compareFn 的措辭,如果範例中有拼字錯誤,就更新範例,並確認教學顯示的是修正後的程式碼片段。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
javascript
領域
documentation
Issue 類型
文件
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。