Skip to content

Commit 100936e

Browse files
committed
update Array 随机排列ES6简写
1 parent a4ae1b4 commit 100936e

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

array-part/README.md

+8-11
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,14 @@ function unique(arr) {
239239
洗牌算法,从最后一个开始随机对换位置。
240240

241241
```
242-
Array.prototype.shuffle = function() {
243-
var input = this;
244-
for (var i = input.length - 1; i >= 0; i--) {
245-
var randomIndex = Math.floor(Math.random() * (i + 1));
246-
var itemAtIndex = input[randomIndex];
247-
248-
input[randomIndex] = input[i];
249-
input[i] = itemAtIndex;
250-
}
251-
return input;
252-
};
242+
Array.prototype.shuffle = function () {
243+
const arr = this
244+
for (let i = arr.length - 1; i >= 0; i--) {
245+
const randomIndex = Math.random() * (i + 1) | 0;
246+
[arr[randomIndex], arr[i]] = [arr[i], arr[randomIndex]]
247+
}
248+
return arr
249+
}
253250
```
254251

255252
创建一个长度为100的数组,并且每个元素的值等于它的下标

0 commit comments

Comments
 (0)