打亂給定數(shù)組,返回新數(shù)組红柱,不修改原數(shù)組
function shuffle(arr) {
let
n = arr.length,
newArr = [...arr]
while (n) {
let m = Math.floor(Math.random() * n-- );
[newArr[m], newArr[n]] = [newArr[n], newArr[m]];
}
return newArr
}
打亂給定數(shù)組,返回新數(shù)組红柱,不修改原數(shù)組
function shuffle(arr) {
let
n = arr.length,
newArr = [...arr]
while (n) {
let m = Math.floor(Math.random() * n-- );
[newArr[m], newArr[n]] = [newArr[n], newArr[m]];
}
return newArr
}