JavaScript:为什么我的While-Loop无限循环?

时间:2017-11-16 22:24:43

标签: javascript while-loop

我有一个while循环,它是某个数组路径的算法,我在javaScript中创建它。这是代码。

static_cast

}

由于我无法解决问题,我无法找出是什么导致这无限循环,但我所知道的是它可能与RV1和RV2的声明有关。谁能告诉我代码中的哪个位置让它永远循环?

更新我添加了完整的代码,以帮助我们进一步了解问题。

1 个答案:

答案 0 :(得分:1)

几点。一,"假"不等于false也不等于true,因为字符串是字符串值,而true / false是布尔值。虽然,您可以编写如下代码:



console.log(new Boolean("false") == true); // true
console.log("false" == String(true));      // false




查看更多信息here

没有包含字符串和数字的混合数组。因此,我使用99代替" X"但您可以使用您认为合适的任何数值。您需要指定一个条件以使循环停止。现在循环不再是无止境的。另请参阅here



var Row1 = [0, 0, 0, 0, 0];
var Row2 = [0, 0, 0, 0, 0];
var Row3 = [0, 0, 0, 0, 0];
var Row4 = [0, 0, 0, 0, 0];
var Row5 = [0, 0, 0, 0, 0];
var AllRows = [];

//Add Rows
AllRows.push(Row1, Row2, Row3, Row4, Row5);
console.log(AllRows);

//First Value
var SV1 = [0, (AllRows.length - 1)];
var SV2 = [0, (AllRows.length - 1)];

var GetSV1 = Math.floor(Math.random()*(SV1.length - 1));
var GetSV2 = Math.floor(Math.random()*(SV2.length - 1));
AllRows[GetSV1][GetSV2] = 1;

var Steps = 1;
var PD = true; // "false" to true
var RV1 = null;
var RV2 = null;

while(PD === true){
    RV1 = Math.floor(Math.random()*(AllRows.length - 1));
    RV2 = Math.floor(Math.random()*(AllRows.length - 1));
    if(AllRows[RV1][RV2] == AllRows[GetSV1][GetSV2] || AllRows[RV1][RV2] == 99){
        continue;
    } else {
        AllRows[RV1][RV2] = 1;
    }

    if(AllRows[0][0] == 0){
        if(Steps >= 1 && AllRows[0][1] == 1){
            AllRows[1][0] = 99;
            Steps++;
            continue;
        } else if(Steps >= 1 && AllRows[1][0] == 1){
            AllRows[0][1] = 99;
            Steps++;
            continue;
        } else{
            AllRows[0][1] = 0;
            AllRows[1][0] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        } 
        if(Steps >= 2 && AllRows[0][2] == 1){
            AllRows[1][1] = 99;
            Steps++;
            continue;
        } else if(Steps >= 2 && AllRows[1][1] == 1){
            AllRows[0][2] = 99;
            AllRows[2][0] = 99;
            Steps++;
            continue;
        } else if(Steps >= 2 && AllRows[2][0] == 1){
            AllRows[1][1] = 99;
            Steps++;
            continue;
        } else{
            AllRows[0][2] = 0;
            AllRows[1][1] = 0;
            AllRows[2][0] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        }
        if(Steps >= 3 && AllRows[0][3] == 1){
            AllRows[1][2] = 99;
            Steps++;
            continue;
        } else if(Steps >= 3 && AllRows[1][2] == 1){
            AllRows[0][3] = 99;
            AllRows[2][1] = 99;
            Steps++;
            continue;
        } else if(Steps >= 3 && AllRows[2][1] == 1){
            AllRows[3][0] = 99;
            AllRows[1][2] = 99;
            Steps++;
            continue;
        } else if(Steps >= 3 && AllRows[3][0] == 1){
            AllRows[2][1] = 99;
            Steps++;
            continue;
        }  else{
            AllRows[0][3] = 0;
            AllRows[1][2] = 0;
            AllRows[2][1] = 0;
            AllRows[3][0] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        }
        if(Steps >= 4 && AllRows[0][4] == 1){
            AllRows[1][3] = 99;
            Steps++;
            continue;
        } else if(Steps >= 4 && AllRows[1][3] == 1){
            AllRows[0][4] = "X";
            AllRows[2][2] = "X";
            Steps++;
            continue;
        } else if(Steps >= 4 && AllRows[2][2] == 1){
            AllRows[3][1] = "X";
            AllRows[1][3] = 99;
            Steps++;
            continue;
        } else if(Steps >= 4 && AllRows[3][1] == 1){
            AllRows[2][2] = 99;
            AllRows[4][0] = "X";
            Steps++;
            continue;
        } 
      else 
        if(Steps >= 4 && AllRows[4][0] == 1){
            AllRows[3][1] = 99;
            PD = false;
        } else{
            AllRows[0][4] = 0;
            AllRows[1][3] = 0;
            AllRows[2][2] = 0;
            AllRows[3][1] = 0;
            AllRows[4][0] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        }
        if(Steps >= 5 && AllRows[1][4] == 1){
            AllRows[2][3] = 99;
            Steps++;
            continue;
        } else if(Steps >= 5 && AllRows[2][3] == 1){
            AllRows[1][4] = 99;
            AllRows[3][2] = 99;
            Steps++;
            continue;
        } else if(Steps >= 5 && AllRows[3][2] == 1){
            AllRows[4][1] = 99;
            AllRows[2][3] = 99;
            Steps++;
            continue;
        } else 
          if(Steps >= 5 && AllRows[4][1] == 1){
            AllRows[3][2] = 99;
            PD = false; //null to false
        } else{
            AllRows[1][4] = 0;
            AllRows[2][3] = 0;
            AllRows[3][2] = 0;
            AllRows[4][1] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        }
        if(Steps >= 6 && AllRows[2][4] == 1){
            AllRows[3][3] = 99;
            Steps++;
            continue;
        } else if(Steps >= 6 && AllRows[3][3] == 1){
            AllRows[2][4] = 99;
            AllRows[4][2] = 99;
            Steps++;
            continue;
        } else if(Steps >= 6 && AllRows[4][2] == 1){
            AllRows[3][3] = 99;
            PD = false;
        } else{
            AllRows[2][4] = 0;
            AllRows[3][3] = 0;
            AllRows[4][2] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        }
        if(Steps >= 7 && AllRows[3][4] == 1){
            AllRows[4][3] = 99;
            Steps++;
            continue;
        } else if(Steps >= 7 && AllRows[4][3] == 1){
            AllRows[3][4] = 99;
            PD = false;
        } else if(Steps >= 7 && AllRows[4][4] == 1){
            PD = false;
        } else{
            AllRows[3][4] = 0;
            AllRows[4][3] = 0;
            AllRows[4][4] = 0;
            AllRows[GetSV1][GetSV2] = 0;
        }
    }
     else{
        break;
    }
    
}




请注意,最好按照MDN严格比较布尔与布尔值。这就是为什么这个例子使用" ==="用于比较的运算符,并将PD显式设置为true或false。