如何在 while 循环中访问变量?

How do i access a variable inside a while loop?

我试图在第二个 while 循环中访问问题 2,但我不能,因为它在第一个 while 循环中处于块范围内。我厌倦了在 while 循环之外声明它,但是 while 循环将不起作用。任何帮助表示赞赏。第二个 while 循环中的 if 语句将不起作用,因为问题 2 在块范围内。

var readlineSync = require("readline-sync");

function run() {
  var question1 = readlineSync.question("Press any key to start the game");

  const randomNumber1 = Math.floor(Math.random() * 3) + 1;
  const randomNumber2 = Math.floor(Math.random() * 3) + 1;

  /*first placement*/
  firststring(1);
  function firststring(stringlength) {
    var emptyString1 = "";
    var emptyString2 = "";
    var characters = "ABC";
    var characters2 = "ABC";
    let operation1 = false;
    let operation2 = false;

    for (var i, i = 0; i < stringlength; i++) {
      emptyString1 += characters.charAt(
        Math.floor(Math.random() * characters.length)
      );
    }
    for (var i, i = 0; i < stringlength; i++) {
      emptyString2 += characters.charAt(
        Math.floor(Math.random() * characters2.length)
      );
    }
    var totalOne = emptyString1 + randomNumber1;
    var totalTwo = emptyString2 + randomNumber2;
    var toString1 = totalOne.toString();
    var toString2 = totalTwo.toString();
    console.log(toString1);
    console.log(toString2);

    if (toString1 === toString2 || totalOne === totalTwo) {
      run();
    }

    let question2;

    while (!operation1) {
      question2 = readlineSync.question('Enter a location to strike ie "A2"');

      if (question2 === toString1 || question2 === toString2) {
        operation1 = true;
        console.log("Hit. You have sunk a battleship. 1 ship remaining.");

        break;
      } else {
        console.log("You have missed!");
      }
    }

    let question3;
    let question4;


    while (!operation2) {
      question3 = readlineSync.question("Enter a location to strike the second battleship")

        if (question3 === toString1 || question3 === toString2) {
          operation2= true;
          console.log('HIT!')
           break;
        } else {
          console.log('You have misssed')
        }


        if (question3 === question2) {
          console.log("You have already entered that value.");
        }
      }
      
              question4 = readlineSync.question(
                "You have destroyed all battleships. Would you like to play again? Y/N"
              );
          
      
      
            if(question4 === "Y" || question4 === "y") {
              run();
            }
    }

}
run();

在上面的代码中,question2 被声明并可用于整个 firststring 函数,并且可用于两个 while 循环。

let in JavaScript 为其当前范围定义一个变量,如下所示:

let a;
let b = 3;
if (true) {
  console.log(b); // 3
  let c = 5;
  b = 4;
  a = 6;
}
console.log(a); // 6
console.log(b); // 4
// console.log(c); // Uncaught ReferenceError: c is not defined

编辑: 使用提供的示例结果,问题是 if (question3 === question2) 检查在“命中检查”(中断循环)之后调用。你需要这个:

while (!operation2) {
  question3 = readlineSync.question("Enter a location to strike the second battleship")
  if (question3 === question2) {
    console.log("You have already entered that value.");
  } else if (question3 === toString1 || question3 === toString2) {
    operation2= true;
    console.log('HIT!')
    break;
  } else {
    console.log('You have misssed')
  }
}