1

我有一个对象数组,其中一个示例如下所示:

var allWorm = [
        {
            "name": "Null",
            "power": "Create an artificial cluster. Everyone added to this cluster has their parahuman abilities shared, but with a decrease in the power of each new ability.",
            "fromCanon": "Null"
        },
        {
            "name": "One",
            "power": "Thinker ability that allows for quick and efficient brainwashing given sufficient control over the victim's environment.",
            "fromCanon": "One"
        },
        {
            "name": "Two",
            "power": "Magnify other powers in close proximity.",
            "fromCanon": "Two"
        },
        {
            "name": "Four",
            "power": "Limited flight. Can hover between five and ten feet from the ground with a top speed of fifty to sixty miles per hour.",
            "fromCanon": "Four"
        },
        {
            "name": "Nine",
            "power": "Can push and pull on metals within a short range, accelerating both self and object away from or towards each other.",
            "fromCanon": "Nine"
        },
        {
            "name": "Thirteen",
            "power": "Forcefield creation.",
            "fromCanon": "Thirteen"
        }
]

在我的 HTML 中,我有一个复选框。如果选中该框,我可以使用 shuffle 函数导致条目被随机打乱,可能会在它开始的相同位置留下一个值。但是,我尝试更改 else 语句中所示的算法会导致出现问题,并且程序会陷入无限循环。

function shuffle(){
    //see if box checked for duplicates
    var samePower = document.getElementById("samePower");
    
    let allWorm = JSON.parse(localStorage.getItem("allWorm"));
    
    var currentIndex = allWorm.length, temporaryValue, randomIndex;
    
    if(samePower.checked == true){
        //while non-shuffled elements remain
        while(0 !== currentIndex){
            //pick remaining element
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
            
            //swap with current element
            temporaryValue = allWorm[currentIndex].power;
            allWorm[currentIndex].power = allWorm[randomIndex].power;
            allWorm[randomIndex].power = temporaryValue;
                
            temporaryValue = allWorm[currentIndex].fromCanon;
            allWorm[currentIndex].fromCanon = allWorm[randomIndex].fromCanon;
            allWorm[randomIndex].fromCanon = temporaryValue;
        }
    }
    else{
        //while non-shuffled elements remain
        while(0 !== currentIndex){
            //pick remaining element
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
            //disallow same power
            while(allWorm[randomIndex].name === allWorm[randomIndex].fromCanon){
                randomIndex = Math.floor(Math.random() * currentIndex);
                break;
            }
            
            //swap with current element
            temporaryValue = allWorm[currentIndex].power;
            allWorm[currentIndex].power = allWorm[randomIndex].power;
            allWorm[randomIndex].power = temporaryValue;
                
            temporaryValue = allWorm[currentIndex].fromCanon;
            allWorm[currentIndex].fromCanon = allWorm[randomIndex].fromCanon;
            allWorm[randomIndex].fromCanon = temporaryValue;
        }
    }
    
    var allWorm_serialized = JSON.stringify(allWorm);
    localStorage.setItem("allWorm", allWorm_serialized);
    writeData();
}

如何正确修改算法,?

4

0 回答 0