微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Fisher Yates 添加重复项

如何解决Fisher Yates 添加重复项

我正在尝试打乱 6 个 T 和 6 个 A 并循环直到到达数组的末尾。但是,在循环播放时,T 和 A 的显示数量一直不均匀。我是 JavaScript 新手,不确定问题是否与随机播放或循环有关。

//function to shuffle word array
function shuffle(array) {
var currentIndex = array.length,temporaryValue,randomIndex;
// remaining elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
    return array;
}
var wordt= ["T","A","T","A"]; //Your words
shuffle(wordt)
var i = 0;
    var currentWordt = 0;
    //pull T/A
    while (12 !==currentWordt) {
    var my_wordt =  wordt[i];
    var nb = jQuery('#NextButton');
    nb.val(my_wordt);
    nb.attr('title',my_wordt);
    i +=1
    currentWordt += 1
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。