简体   繁体   English

Javascript嵌套的while循环中带有continue语句的行为不符合预期

[英]Javascript nested while loop with continue statement not behaving as expected

I'm trying to find all permutations of 3 arrays using nested while loops and the continue statement. 我正在尝试使用嵌套的while循环和continue语句找到3个数组的所有排列。 It's almost working as I'd like, but is adding an extra element when control is given back to the outerloop. 它几乎可以按照我的意愿工作,但是在将控制权交还给外部循环时会添加一个额外的元素。 I'm going to rewrite it using recursion, but would like to know why it's doing this. 我将使用递归重写它,但想知道为什么要这样做。 Here's a link: http://jsbin.com/fuyup/15/edit 这是链接: http : //jsbin.com/fuyup/15/edit

Thanks for any advice. 感谢您的任何建议。

function findPermutations() {
    var g1 = ['a1', 'a2'],
        g2 = ['b1', 'b2', 'b3'];
        g3 = ['c1', 'c2', 'c3', 'c4'];

    var g1p = 0,
        g2p = 0, 
        g3p = 0,
        g1len = g1.length,
        g2len = g2.length,
        g3len = g3.length, 
        temp = [],
        result = [];

    outerloop: while (g1p < g1len) {
        temp.push(g1[g1p]);

        while (g2p < g2len) {
            temp.push(g2[g2p]);

            while (g3p < g3len) {
                temp.push(g3[g3p]);
                result.push(temp);
                temp = [];  
                g3p++;
                continue outerloop;
            }

            g3p = 0;
            g2p++;
        }

        g2p = 0;
        g1p++;
    }
    return result;
}

With that continue and the spare temp = [] reset your control flow is really messed up. continue然后将备用temp = []重置,您的控制流程确实搞砸了。

Before g3p = 0; 之前g3p = 0; is executed and the innermost loop (that pushes to result and resets temp ), the flow jumps back to temp.push(g2[g2p]); 执行并执行最里面的循环(推入结果并重置temp ),流程跳回到temp.push(g2[g2p]); which is called twice on a the same temp array. 在同一个temp数组上被两次调用。 I'm not going into detail here… If you need to know, use a debugger and step through the execution. 我在这里不做详细介绍...如果您需要知道,请使用调试器并逐步执行。

I've made a working version that uses continue , but I'm not really lucky with it: 我已经制作了一个使用continue的工作版本,但是我并不是很幸运:

outerloop: while (g1p < g1len) {
    temp.push(g1[g1p]);
    while (g2p < g2len) {
        temp.push(g2[g2p]);
        while (g3p < g3len) {
            temp.push(g3[g3p]);

            result.push(temp);

            temp = [];  
            g3p++;
            continue outerloop; 
        }
        g3p = 0;

        temp = [];
        g2p++;
        continue outerloop; 
    }
    g2p = 0;

    temp = [];
    g1p++;
    // continue outerloop; (implicit)
}

You can see the symmetric structure here, on every level the same things happen. 您可以在这里看到对称结构,在每个级别上都发生相同的事情。 And whenever we enter the outerloop to get the sequence 并且每当我们进入外outerloop获取序列时

temp.push(g1[g1p]); temp.push(g2[g2p]); temp.push(g3[g3p]); result.push(temp);

executed, we reset temp before. 执行后,我们之前会重置temp


The usual idea to create permutations is to use no continue , but nest normal for loops that mutate an array, and take snapshots of each state to append to the result: 创建置换的通常想法是不使用continue ,而将正常嵌套嵌套for使数组变异的循环,并拍摄每个状态的快照以追加到结果中:

temp = [];
g1p = 0;
while (g1p < g1len) {
    temp[0] = g1[g1p];

    g2p = 0;
    while (g2p < g2len) {
        temp[1]= g2[g2p];

        g3p = 0;
        while (g3p < g3len) {
            temp[2] = g3[g3p];

            result.push(temp.slice());

            g3p++;
        }
        g2p++;
    }
    g1p++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM