fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

手写洗牌算法

Open habc0807 opened this issue 4 years ago • 1 comments

habc0807 avatar Sep 01 '20 14:09 habc0807

function FYShuffle (arr) {
    let len = arr.length;
    
    while (len > 1) {
        let rand = Math.floor(Math.random() * len);
        len--;
        [arr[len], arr[rand]] = [arr[rand], arr[len]] // 采用的数组的结构赋值
    }

    return arr;
}
console.log(FYShuffle([1,2,3,4,5,6]))

habc0807 avatar Sep 02 '20 03:09 habc0807