简体   繁体   English

无法弄清楚这种行为:while 循环中的 2 个 while 循环

[英]Can't figure out this behavior: 2 while loops inside a while loop

I'm doing a kata for Codewars that puts two arrays of numbers up against each other.我正在为Codewars做一个 kata,它将两个 arrays 的数字放在一起。 The "opponent" array always has on average larger numbers than the "codewarrior" array, and both arrays are always the same length. “对手”数组的平均数字总是比“codewarrior”数组大,而且 arrays 的长度总是相同的。 What I need to do is find the most efficient way to get victories (codewarrior[x] > opponent[y]), stalemates (codewarrior[x] == opponent[y]) if a victory isn't possible, or defeats (codewarrior[x] < opponent[y]) if neither a victory or stalemate is possible.我需要做的是找到最有效的方法来获得胜利(codewarrior[x] >对手[y]),如果无法取得胜利或失败( codewarrior[x] <对手[y]) 如果胜利或僵局都不可能。 Before anyone asks, I don't want the solution to the kata, only how to get my program to work.在有人问之前,我不想要 kata 的解决方案,只想要如何让我的程序工作。

function codewarResult(codewarrior, opponent) {
    codewarrior = codewarrior.sort(function(a, b){return a - b});
    opponent = opponent.sort(function(a, b){return a - b});

    console.log("Ordered codewarrior array: ", codewarrior);
    console.log("Ordered opponent array:", opponent);

    let victories = 0;
    let stalemates = 0;
    let defeats = 0;
    
    let x = 0;
    while(x < codewarrior.length) {
        let y = 0;
        while(codewarrior[x] > opponent[y]) { // Victory loop (most preferable)
            if(codewarrior[x] <= opponent[y + 1]) {
                victories++;
                codewarrior.splice(codewarrior.indexOf(x), 1, null); // I replace the value to null so the array retains it's length
                opponent.splice(opponent.indexOf(y), 1);
                console.log(`Codewarrior array after victory: `, codewarrior);
                console.log(`Opponent array after defeat: `, opponent);
            }
            y++;
        }
        if(codewarrior[x] == opponent[y]) { // Stalemate checker (second most preferable)
            stalemates++;
            codewarrior.splice(codewarrior.indexOf(x), 1, null);
            opponent.splice(opponent.indexOf(y), 1);
            console.log(`Codewarrior array after stalemate: `, codewarrior);
            console.log(`Opponent array after stalemate: `, opponent);
        }
        while(codewarrior[x] < opponent[y]) { // Defeat loop (least preferable)
            if(codewarrior[x] >= opponent[y + 1]) {
                defeats++;
                codewarrior.splice(codewarrior.indexOf(x), 1, null);
                opponent.splice(opponent.indexOf(y), 1);
                console.log(`Codewarrior array after defeat: `, codewarrior);
                console.log(`Opponent array after victory: `, opponent);
            }
            y++;
        }
        x++;
    }
    
    console.log(`victories: ${victories}, stalemates: ${stalemates}, defeats ${defeats}.`);
    
    if(victories > defeats) {
      return "Victory";
    } else if(victories === defeats) {
      return "Stalemate";
    } else {
      return "Defeat";
    }
}

Above I order the two arrays ordered from smallest to largest.上面我订购了从小到大排列的两个 arrays。 I then have one large while loop that iterates the "codewarrior" array, and two while loops and an if statement inside iterating the "opponent" array each time that checks for a possible victory first, a stalemate second, and a defeat last.然后我有一个大的while循环来迭代“codewarrior”数组,两个while循环和一个if语句在每次迭代“对手”数组时首先检查可能的胜利,然后是僵局,最后是失败。 It checks for the most efficient way to get a victory (the values of codewarrior[x] > opponent[y] as close as possible) a stalemate, or a defeat (the values of codewarrior[x] < opponent[y] as far apart as possible).它检查获得胜利的最有效方法(codewarrior[x] >尽可能分开)。

When I try当我尝试

codewarResult([4,3,2,1], [5,4,3,2]);

I expect the two arrays to have 4 "battles" against each other, with the values of "codewarrior" slowly becoming null as the large while loop iterates through it.我希望两个 arrays 彼此有 4 次“战斗”,随着大型 while 循环迭代,“codewarrior”的值慢慢变为 null。 Instead I get this behavior in the console:相反,我在控制台中得到了这种行为:

Ordered codewarrior array:  [ 1, 2, 3, 4 ]
Ordered opponent array: [ 2, 3, 4, 5 ]
Codewarrior array after stalemate:  [ null, 2, 3, 4 ]
Opponent array after stalemate:  [ 2, 3, 4 ]
Codewarrior array after victory:  [ null, null, 3, 4 ]
Opponent array after defeat:  [ 2, 3 ]
Codewarrior array after stalemate:  [ null, null, 3, null ]
Opponent array after stalemate:  [ 2 ]
victories: 1, stalemates: 2, defeats 0.

Why is there only 3 battles being recorded and the 3 in the "codewarriors" array being skipped?为什么只记录了 3 场战斗,而跳过了“codewarriors”数组中的 3 场? Why does the first battle result in a stalemate when it should be a defeat (1 vs 5)?为什么第一场比赛本应该是失败(1对5)却陷入胶着?

Couple of things...几件事...

Your outer loop goes through all of your codeWarrior numbers... That makes sense...你的外循环遍历你所有的 codeWarrior 号码......这是有道理的......

Your inner loops for victory and defeat though are odd... it looks like if codeWarrior[0] is greater than opponent[0] then it should be a victory right?你的胜利和失败的内在循环虽然很奇怪......看起来如果 codeWarrior[0] 大于对手 [0] 那么它应该是胜利吧? But your victories counter only gets incremented if codeWarrior[0] will lose/stalemate against opponent[1].但是只有当 codeWarrior[0] 输掉/与对手 [1] 相持时,你的胜利计数器才会增加。 Same deal for defeat... if codeWarrior[0] loses to opponent[0], it only counts a defeat if codeWarrior[0] would win/stalemate with opponent[1].同样的失败交易...如果 codeWarrior[0] 输给了对手 [0],则只有在 codeWarrior[0] 与对手 [1] 获胜/僵持时才算失败。

As for not having the number of battles you think you should... look here:至于没有你认为应该的战斗次数……看这里:

if(codewarrior[x] == opponent[y]) { // Stalemate checker (second most preferable)
stalemates++;
codewarrior.splice(codewarrior.indexOf(x), 1, null);
opponent.splice(opponent.indexOf(y), 1);

You are probably meaning to remove a single number from codeWarrior at index x and single number from opponent at index y.您可能打算从索引 x 处的 codeWarrior 中删除单个数字,并从索引 y 处的对手中删除单个数字。 By using indexOf() however, you are saying find the index in codeWarrior where the warrior's number == x and remove that element.但是,通过使用 indexOf(),您是说在 codeWarrior 中找到战士编号 == x 的索引并删除该元素。 If the value of X isnt found as a number in the codeWarrior at all,then nothing happens.如果在 codeWarrior 中根本没有找到 X 的值作为数字,那么什么也不会发生。 Same for opponent and indexOf(y).对手和 indexOf(y) 相同。 As a result, you are having most likely the wrong items being removed from the arrays or not removed if they are supposed to.因此,您很可能从 arrays 中删除了错误的项目,或者如果它们应该删除则没有删除。

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

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