[问答题]

```function shuffle(arr) {     //***
}```

# 5个回答

function shuffle(arr) {
var temp = null
for(var i = 0; i < arr.length; i++) {
var rn = Math.floor(Math.random()*arr.length)
if(rn != i) {
temp = arr[i];
arr[i] = arr[rn];
arr[rn] = temp
}
return arr
}
}

function shuffle(arr) {
if(this.circut == undefined){
this.circut =0
}
this.circut += 1
console.log('this,circut',this.circut)
var length = arr.length
var arr1=arr.slice(0,length/2)
var arr2=arr.slice(length/2,length)
var arrnow = [ ]
for (var i=0;i<length/2;i++){
arrnow.push(arr1[i])
arrnow.push(arr2[i])
}
if(arrnow.indexOf(undefined)==-1 && this.circut<3){
return shuffle(arrnow)
}else if(this.circut<3){
arrnow.splice(arrnow.indexOf(undefined),1)
return shuffle(arrnow)
}
if(this.circut==3){
return arrnow
}
}

 1 2

```function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
function shuffle (arr) {
let _arr = arr.slice();
for (let i = 0 ; i < _arr.length; i++) {
let j = getRandomInt(0, i);
let temp = _arr[i];
_arr[i] = _arr[j];
_arr[j] = temp;
}
return _arr;
}```

```
functionshuffle (arr) {

varres = [];

for(vari = 0; i < 3; i++) {

res[i] = [];

for(varj = 0; j < 13; j++){

varnum = random();

if(hasValue(arr, num)) {

varindex = arr[hasValue(arr,num)];

res[i][j] = arr[index];

arr.splice(index, 1);

} else{

}

}

}

}

}

functionhasValue (arr, num) {

let num = null;

arr.forEach((item, index) => {

if(item === num) {

returnindex;

}

});

returnnull;

}

// 生成1-13的随机数

functionrandom() {

retun Math.round(Math.random()*(13-1) + 1);

}

```

```function shuffle(arr) {
var arr_1 = [];
while (arr.length > 0){
var index = Math.round(Math.random()*(arr.length - 1));
arr_1.push(arr[index]);
arr.splice(index, 1);
}
return arr_1;
} 🤣🤣🤣 ```

5条回答 357浏览

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题