簡體   English   中英

無法弄清楚這種行為:while 循環中的 2 個 while 循環

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

我正在為Codewars做一個 kata,它將兩個 arrays 的數字放在一起。 “對手”數組的平均數字總是比“codewarrior”數組大,而且 arrays 的長度總是相同的。 我需要做的是找到最有效的方法來獲得勝利(codewarrior[x] >對手[y]),如果無法取得勝利或失敗( codewarrior[x] <對手[y]) 如果勝利或僵局都不可能。 在有人問之前,我不想要 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";
    }
}

上面我訂購了從小到大排列的兩個 arrays。 然后我有一個大的while循環來迭代“codewarrior”數組,兩個while循環和一個if語句在每次迭代“對手”數組時首先檢查可能的勝利,然后是僵局,最后是失敗。 它檢查獲得勝利的最有效方法(codewarrior[x] >盡可能分開)。

當我嘗試

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

我希望兩個 arrays 彼此有 4 次“戰斗”,隨着大型 while 循環迭代,“codewarrior”的值慢慢變為 null。 相反,我在控制台中得到了這種行為:

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.

為什么只記錄了 3 場戰斗,而跳過了“codewarriors”數組中的 3 場? 為什么第一場比賽本應該是失敗(1對5)卻陷入膠着?

幾件事...

你的外循環遍歷你所有的 codeWarrior 號碼......這是有道理的......

你的勝利和失敗的內在循環雖然很奇怪......看起來如果 codeWarrior[0] 大於對手 [0] 那么它應該是勝利吧? 但是只有當 codeWarrior[0] 輸掉/與對手 [1] 相持時,你的勝利計數器才會增加。 同樣的失敗交易...如果 codeWarrior[0] 輸給了對手 [0],則只有在 codeWarrior[0] 與對手 [1] 獲勝/僵持時才算失敗。

至於沒有你認為應該的戰斗次數……看這里:

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

您可能打算從索引 x 處的 codeWarrior 中刪除單個數字,並從索引 y 處的對手中刪除單個數字。 但是,通過使用 indexOf(),您是說在 codeWarrior 中找到戰士編號 == x 的索引並刪除該元素。 如果在 codeWarrior 中根本沒有找到 X 的值作為數字,那么什么也不會發生。 對手和 indexOf(y) 相同。 因此,您很可能從 arrays 中刪除了錯誤的項目,或者如果它們應該刪除則沒有刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM