【问题标题】:JavaScript: Shuffling a deck, but it does NOT create a new shuffled deck each time using the original listJavaScript:洗牌,但它不会每次使用原始列表创建一个新的洗牌
【发布时间】:2021-07-20 11:38:26
【问题描述】:

我正在尝试洗牌。我将一些卡片作为输入。例如,numCards = 5,所以牌组(列表)变为 [0,1,2,3,4]。问题是 test_order(i,j,l) 函数中的 while 循环没有正确改组。函数 console.log(m) 应该使用原始列表 (l) 打印一个新的洗牌甲板/列表,但它会在第一次正确洗牌后继续打印 [0,1,2,3,4]。它应该每次使用原始列表创建一个新洗牌的牌组,相反,它会不断重复原始列表或第一个洗牌列表。

该程序的目的是找出标记为 i 的牌在洗牌后高于标记为 j 的牌的次数的概率。

function list(numCards){
    var newList = []
    var i = 0;
    while (i < numCards){
        newList.push(i);
        i++;
    }
    return newList
}

function top_to_random(l){
    while(numPerformed != 0){
        var x = Math.floor(Math.random() * l.length);
        l.splice(x, 0, l.shift())
        numPerformed--;
    }
    return l
}

function test_order(i,j,l){ //PROBLEM IS IN HERE!!!!
    var n = 0
    var trials = 10
    var count = 0
    while (count < trials){ // PROBLEM IN WHILE LOOP
        let arrayCopy = JSON.parse(JSON.stringify(l));
        //console.log(arrayCopy)
        var m = top_to_random(arrayCopy)
        m.indexOf(i) < m.indexOf(j) ? n++ : n = n + 0
        
        //console.log(arrayCopy)
        console.log(m)
        count++
    }
    var prob = n/trials
    return  prob
}

//Argument Inputs
var numCards = parseInt(prompt("Enter number of cards in deck: "))
var l = list(numCards)
var numPerformed = parseInt(prompt("Enter number of shuffles to perform on deck: "));
var i = parseInt(prompt("i: "))
var j = parseInt(prompt("j: "))

//Execution
console.log(test_order(i,j,l))

【问题讨论】:

    标签: javascript arrays while-loop shuffle


    【解决方案1】:

    问题不在于您认为的问题,而在于您的top_to_random 函数。您从numPerformed 开始计算随机播放中完成的混音次数,但这是一个全局范围的变量,因此不会在每次调用时重置。您应该将混合计数作为参数传递,如下所示:

    function top_to_random(l, mixNum){
        for (;mixNum > 0; mixNum--) {
            var x = Math.floor(Math.random() * l.length);
            l.splice(x, 0, l.shift());
        }
        return l;
    }
    

    修复了一些你的语法微结构,我得到了这个代码:

    function list(numCards){
        var newList = [];
        var i = 0;
        while (i < numCards){
            newList.push(i);
            i++;
        }
        return newList;
    }
    
    function top_to_random(l, mixNum){
        for (;mixNum > 0; mixNum--) {
            var x = Math.floor(Math.random() * l.length);
            l.splice(x, 0, l.shift());
        }
        return l;
    }
    
    function test_order(i,j,l){ //Problem is NOT in here
        let n = 0;
        let trials = 10;
        for (let count = 0; count < trials; count++) { // No not here
            let arrayCopy = [...l];
            top_to_random(arrayCopy, numPerformed);
            console.log(arrayCopy)
            if (arrayCopy.indexOf(i) < arrayCopy.indexOf(j)) n++;
            
            console.log(arrayCopy);
        }
        var prob = n/trials;
        return  prob;
    }
    
    // Argument Inputs
    var numCards = parseInt(prompt("Enter number of cards in deck: "));
    var l = list(numCards);
    var numPerformed = parseInt(prompt("Enter number of shuffles to perform on deck: "));
    var i = parseInt(prompt("i: "));
    var j = parseInt(prompt("j: "));
    
    //Execution
    console.log(test_order(i,j,l));

    另外,您在编码时应该更加注意细节:

    • 您缺少很多分号
    • 您正在混合函数参数和全局变量,而决策没有逻辑
    • 当你应该使用for时不要使用while
    • 执行简单 if 的三元运算符?
    • 您最好使用constlet 而不是var。一方面,它会为您节省此错误

    更好的编写代码:

    const SHUFFLE_REPEATS = 10;
    
    function list(numCards) {
        const newList = [];
        for (let i = 0; i < numCards; i++)
            newList.push(i);
        return newList;
    }
    
    function top_to_random(l, mixNum) {
        for (; mixNum > 0; mixNum--) {
            const x = Math.floor(Math.random() * l.length);
            l.splice(x, 0, l.shift());
        }
        return l;
    }
    
    function test_order(i, j, l) {
        let n = 0;
        for (let count = 0; count < SHUFFLE_REPEATS; count++) {
            const arrayCopy = [...l];
            top_to_random(arrayCopy, numPerformed);
            console.log(arrayCopy)
            if (arrayCopy.indexOf(i) < arrayCopy.indexOf(j)) n++;
        }
        return n / SHUFFLE_REPEATS;
    }
    
    // Argument Inputs
    const numCards = parseInt(prompt("Enter number of cards in deck: "));
    const l = list(numCards);
    const numPerformed = parseInt(prompt("Enter number of shuffles to perform on deck: "));
    const i = parseInt(prompt("i: "));
    const j = parseInt(prompt("j: "));
    
    //Execution
    console.log(test_order(i,j,l));

    【讨论】:

    • 非常感谢您的详细解释。您能否解释一下 -> let arrayCopy = [...l];做什么?
    • @NewbieCoder 它是应用于数组的扩展运算符,它返回一个克隆数组,例如对包含相同元素的数组的不同引用。
    猜你喜欢
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多